forms in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
acwe88
Forum Newbie
Posts: 24
Joined: Tue Nov 07, 2006 10:47 am

forms in php

Post by acwe88 »

Hi there,

I have a form but i need it so when a user fills in the form and makes a mistake the values are still there

so if user adds there address and clicks submit but his email is wrong that user doesnt have to fill out his address in the form again.

ANy ideas

Thanks
Alex
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It's just a case of using the form field's value attribute:

Code: Select all

<input type="text" name="email" value="<?php echo $email; ?>" />
Have a read in the manual on sessions so that you can keep the data stored for the duration of that user's time on your website.

http://uk2.php.net/manual/en/ref.session.php
obiron
Forum Newbie
Posts: 15
Joined: Fri Nov 10, 2006 4:50 am

Post by obiron »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


When you post the form all the values in the fields are in the $_POST array.  

when you validate the form and redisplay it you need to set the value to be the $_POST value.

Code: Select all

print "<input type=\"text\" name=\"surname\" size=\"20\" value=$_POST['surname']>* Surname cannot be blank"
Obviously you cannot do this on the first posting because the $_POST array elements will not exist so you will need to either create them or use conditional code output

Code: Select all

if (!$_POST['surname'])
{
  $surname = "";
  $errmsg = "";
}
If($_POST['surname'] == "")
{
  $surname=$_POST['surname'];
  $errmsg = "* Surname cannot be blank";
}
print "<input type=\"text\" name=\"surname\" size=\"20\" value=$surname>$errmsg";

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

you want to sanitize all user-submitted data though...

Code: Select all

$surname = sanitize($_POST['surname']); // sanitize is just a figurative function (not real) - look up php data sanitization on google
Post Reply