Tutorial :Easiest way to persist data on a PHP page


I a web application that I am developing (being developed in PHP) when a user posts any request for service the user has to fill in a very length form.

Once user fills in the form and submits it another page (confirmation page) is loaded in which all values filled in by user are show. I must point out that at this stage the data is not saved to MySQL yet. Now on this page the user can either save the service request or go back.

My problem starts here. When the user goes back. All data filled in is lost. BTW User tend to use Browsers Back button instead of the back button provided on the confirmation page.

How can is persist data filled in by user so that user can edit/make changes to data and again submit changed data.


u can use Code:


That works well for me. Keep in mind this line must go before session_start(). Anyway, this is useful for me because I don't have to deal with all the session variables that comes with sessions.

OR put the form data into a session like so: Code:

$_SESSION['myFormData'] = $_POST;  


If you aren't persisting it to the DB yet, one thing you could do is potentially store the values in the user session. So when the user goes forward, save the information into the session, and then when they hit back, load the information from the session. Then when they are finally sure with the dat, you can send it to the DB.


Probably the easiest way would be to store the data in the session. Have your data entry form check the session to see if the data is there, and if it is, pre-populate the form with that data.

Once they complete the process and the data is stored in the DB, clear those values from the session.


Using cookies and JavaScript you could:

  1. store all the values in a cookie when the form is submitted
  2. use the onload handler to check for the cookie and populate the form with the data found in the cookie

Don't forget to code it in a way that allows users without cookies or JavaScript to use the page; they just don't get to have the form populated for them.


Every once in a while someone comes up with a good solution. This is that time.

HTML 5 offline storage is supported now in all major browsers since IE8. With that in mind, I decided to tackle form state once and for all so all scripting languages and even HTML pages can keep form state. No cookies needed, no memory-hogging sessions needed, no server-side programming needed and you can partially fill the form out go where you want to whatever URL, come back and the form is there where you left off. Just drop some small script and you are done.

Here it is for free -> http://www.jasonsebring.com/dumbFormState

Let me know what you think.


Use sessions. They're very easy to use.


On your confirmation page you can put a form with hidden fields that contain all values and a Back submit button which will post them to the previous page. You could also have a second submit button on this form which will post to an url that will write the values to the database.


I did a wizard a few years ago where one of the requirements was that the user should be able to go next/previous and even jump back and forth to steps that had been completed, so if the user had completed step 4 and jumped 2 steps back by clicking previous 2 times he/she should be able to click on step 4 on the "wizard progressor" and jump to step 4. For this I used Serializing objects - objects in sessions and stored the "wizard object" in a database, worked pretty good iirc.

EDIT: Ahh, yes. Another requirements was that the user should be able to return and complete the wizard several weeks later or continue an abandon wizard if there existed one for that users mail address.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »