How to retain values in form if validation fails

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
shubham.amola
Forum Newbie
Posts: 8
Joined: Mon Jan 19, 2009 11:57 pm

How to retain values in form if validation fails

Post 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???
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: How to retain values in form if validation fails

Post 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
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How to retain values in form if validation fails

Post 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.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: How to retain values in form if validation fails

Post 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.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How to retain values in form if validation fails

Post by mattpointblank »

Exactly, hence the @ sign, so if they get sent back to the page, it will show what they typed.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: How to retain values in form if validation fails

Post 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. :)
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How to retain values in form if validation fails

Post 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.
shubham.amola
Forum Newbie
Posts: 8
Joined: Mon Jan 19, 2009 11:57 pm

Re: How to retain values in form if validation fails

Post by shubham.amola »

itz done..thnx 4r ur help
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: How to retain values in form if validation fails

Post 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
Post Reply