Page 1 of 1

How to retain values in form if validation fails

Posted: Tue Jan 20, 2009 12:04 am
by shubham.amola
hii..i ve made a registration form. When user click the submit button all feild values will be checked from the database. If the entered email id is already in the database then it will go back to the registration form with error message. But the problem is wen it goes back to page user has to enter the values again which i dont want to. I want the values to be there so that user can enter only email id and he dnt ve to fill other fields. So any suggestions???

Re: How to retain values in form if validation fails

Posted: Tue Jan 20, 2009 3:09 am
by Stryks
When the form is submitted, you can have the form submit to itself, and handle any processing before the form is displayed. That way, if the form needs to be re-displayed, you can simply access the values in the $_POST array. If the form is fine and processing succeeds, just redirect to the next page.

Cheers

Re: How to retain values in form if validation fails

Posted: Tue Jan 20, 2009 3:47 am
by mattpointblank
Yep. Set form fields like so:

<input name="email" value="<?php echo @$_POST['email']; ?>" type="text" />

(the @ sign means only display if it exists, so it doesn't show a warning message if they haven't submitted it yet.

Re: How to retain values in form if validation fails

Posted: Tue Jan 20, 2009 3:49 am
by papa
mattpointblank wrote:Yep. Set form fields like so:

<input name="email" value="<?php echo @$_POST['email']; ?>" type="text" />

(the @ sign means only display if it exists, so it doesn't show a warning message if they haven't submitted it yet.
?

If not submitted the var is empty.

Re: How to retain values in form if validation fails

Posted: Tue Jan 20, 2009 3:55 am
by mattpointblank
Exactly, hence the @ sign, so if they get sent back to the page, it will show what they typed.

Re: How to retain values in form if validation fails

Posted: Tue Jan 20, 2009 3:58 am
by papa
mattpointblank wrote:Exactly, hence the @ sign, so if they get sent back to the page, it will show what they typed.
I don't get an error message either way but maybe I've missed the point as usual. :)

Re: How to retain values in form if validation fails

Posted: Tue Jan 20, 2009 4:12 am
by mattpointblank
I guess it depends on your settings, but on some servers I get a "$_POST is not set" message when I load the page first time.

Re: How to retain values in form if validation fails

Posted: Tue Jan 20, 2009 4:12 am
by shubham.amola
itz done..thnx 4r ur help

Re: How to retain values in form if validation fails

Posted: Tue Jan 20, 2009 4:18 am
by papa
mattpointblank wrote:I guess it depends on your settings, but on some servers I get a "$_POST is not set" message when I load the page first time.
Ok, well good to know. thx