POST Form Memory?

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
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

POST Form Memory?

Post by timWebUK »

Wasn't exactly sure where to post this, but I have an issue regarding a registration form that gives the user feedback if they enter anything incorrectly. (Email in use/username taken/invalid email/passwords do not match, etc)

However, when it redirects back to the form obviously the form is now empty and the user would have to enter everything again - that would be frustrating for them.

What would be the best way to go about doing this? Storing all the form data (except passwords) in session variables and then populating the form again? This is the only way I can think of without redesigning my entire form. (I do not POST the form data to the same page the form is on which is what I've read elsewhere on the forum, I POST to a separate PHP file and then return to the registration form if there is a problem)

Also, just quickly, in order to highlight the violating fields on the form, I assume CSS would be best way to do this? (I'm not the best at design haha)

Any ideas and tips are welcome! Thanks.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: POST Form Memory?

Post by Eran »

Post to the same page as the form and use the $_FORM variable to repopulate the fields.
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: POST Form Memory?

Post by timWebUK »

$_FORM variable? Huh.. :?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: POST Form Memory?

Post by Eran »

Sorry, I meant the $_POST superglobal :) slip of tongue
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: POST Form Memory?

Post by AbraCadaver »

pytrin wrote:Sorry, I meant the $_POST superglobal :) slip of tongue
You type with your tongue? Wicked!
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: POST Form Memory?

Post by timWebUK »

Ha thought so... is there no other way I could implement for now? Rather than reimplementing my entire submitting form process >__>
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: POST Form Memory?

Post by Eran »

Why would you need to reimplement your form handling process? you can include files in other files.
You type with your tongue? Wicked!
It's not a problem once you get used to it ;)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: POST Form Memory?

Post by AbraCadaver »

timWebUK wrote:Ha thought so... is there no other way I could implement for now? Rather than reimplementing my entire submitting form process >__>
If errors are encountered then I would store the vars as session vars on the processing page and redirect. Check for these vars on the form page and if set then use them. As for the error messages, you can add other session vars before redirect and use those for classes of the inputs and style them red or whatever. Additionally you could set an error message var, check for it in the form and display it next to the fields.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: POST Form Memory?

Post by timWebUK »

Yeah I've already got all that setup, it's purely repopulating the form with the details they entered previously and then marking the violating fields.

At the moment I display an error and they have to fill out the entire form again.
bullbreed
Forum Newbie
Posts: 1
Joined: Thu Jan 07, 2010 3:51 pm

Re: POST Form Memory?

Post by bullbreed »

You could set a variable for every field in the form then use

<?php echo $variable; ?>

in the value of each form field.
Then you user wont have to fill in all the info again.
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: POST Form Memory?

Post by timWebUK »

Yeah I probably will! In future, when I remake this web application I will definitely POST the form to itself, this seems to have many benefits. Are there any drawbacks people know of?
ribble1001
Forum Newbie
Posts: 1
Joined: Sat Dec 26, 2009 5:37 am

Re: POST Form Memory?

Post by ribble1001 »

The only drawbacks to using session variables to store form fields may be one of security; one way round this is to use cookies in conjunction with sessions.
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: POST Form Memory?

Post by SimpleManWeb »

Another solution (and a better solution) would be to check the variable values using Javascript. Doing it this way means that you never have to worry about where the information gets posted to because it will not get posted until all of the fields are filled in correctly. If you're not familiar with Javascript, then you could also check the posted values using your current code, and then if they aren't correct, use a header redirect to go back to your form and pass the correct variables in the URL. Something like index.php?id=3&fname=John&lname=Doe..... Then, on your form you could use code like:

Code: Select all

 
   $FName = $_GET['fname'];
   $LName = $_GET['lname'];
 
Those values could then be used like:

Code: Select all

 
     <input type="text" name="fname" value="<?php echo $FName; ?>" />
 
Hope this helps
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: POST Form Memory?

Post by pickle »

Using Javascript is good - if the user has Javascript. You have to consider not everyone will have Javascript turned on, so you're back to square one.

One thing you might want to consider is using $_GET. Put what the user entered in the URL you redirect them back to, and in your form, auto-populate from $_GET if possible.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply