Page 1 of 1

forms in php

Posted: Thu Nov 30, 2006 5:05 am
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

Posted: Thu Nov 30, 2006 6:42 am
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

Posted: Thu Nov 30, 2006 10:04 am
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]

Posted: Thu Nov 30, 2006 10:34 am
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