Page 1 of 1
POST Form Memory?
Posted: Thu Jan 07, 2010 8:08 am
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.
Re: POST Form Memory?
Posted: Thu Jan 07, 2010 9:48 am
by Eran
Post to the same page as the form and use the $_FORM variable to repopulate the fields.
Re: POST Form Memory?
Posted: Thu Jan 07, 2010 9:54 am
by timWebUK
$_FORM variable? Huh..

Re: POST Form Memory?
Posted: Thu Jan 07, 2010 10:31 am
by Eran
Sorry, I meant the $_POST superglobal

slip of tongue
Re: POST Form Memory?
Posted: Thu Jan 07, 2010 10:55 am
by AbraCadaver
pytrin wrote:Sorry, I meant the $_POST superglobal

slip of tongue
You type with your tongue? Wicked!
Re: POST Form Memory?
Posted: Thu Jan 07, 2010 11:21 am
by timWebUK
Ha thought so... is there no other way I could implement for now? Rather than reimplementing my entire submitting form process >__>
Re: POST Form Memory?
Posted: Thu Jan 07, 2010 11:27 am
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

Re: POST Form Memory?
Posted: Thu Jan 07, 2010 12:53 pm
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.
Re: POST Form Memory?
Posted: Thu Jan 07, 2010 1:08 pm
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.
Re: POST Form Memory?
Posted: Thu Jan 07, 2010 4:04 pm
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.
Re: POST Form Memory?
Posted: Thu Jan 07, 2010 4:43 pm
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?
Re: POST Form Memory?
Posted: Thu Jan 07, 2010 4:53 pm
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.
Re: POST Form Memory?
Posted: Thu Jan 07, 2010 8:10 pm
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
Re: POST Form Memory?
Posted: Fri Jan 08, 2010 3:20 pm
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.