q0987
q0987

Reputation: 35982

php - How to save the form data temporary

I have designed a user profile form, a long form which is quite complicated. Before the user can submit and save it, the form needs to be validated.

Here is the question:

When the form is saved into the DB, there are multiple tables involved. Now I would like to introduce a function called "Save Form" which internally doesn't validate the form and just save whatever the user entered.

There are potential issues, such as:

Q1> How to save temporary data?

Q2> What should I do if the user saves the temporary data without submitting the form and then quit. This feature looks like the Draft feature provided by Gmail.

Upvotes: 5

Views: 12466

Answers (2)

Nate Pinchot
Nate Pinchot

Reputation: 3318

Q1> How to save temporary data?

There are a couple ways you could do this. You could save it to a different table. You could also (probably more popular) save it to the same table as the completed profiles, but flag it as incomplete via a column for this purpose. This column could be named complete. If the profile is not complete, it would be set to 0 or false. If it is complete, it would be set to 1 or true. You can then query based on it.

You could then "validate" the data via whatever procedure is required and set the column to 1 or true as needed.

Q2> What should I do if the user saves the temporary data without submitting the form and then quit. This feature looks like the Draft feature provided by Gmail.

The answer above should serve this purpose as well.

If you need to reload the data and allow the user to continue filling out the incomplete form, you will need to get some sort of identifier for them to allow this. You could do this by setting a cookie, or by some sort of unique identifier they would enter at the top of the form - such as SSN, phone #, student ID #, etc (or a combination).


If you are looking for a feature like Gmail's draft feature, you will need to implement some sort of AJAX call that automatically fire's every X seconds/minutes to automatically save the data.

Upvotes: 7

david
david

Reputation: 33517

You could create a session for the fields you're interested in saving. For example

$this_form = array(
  'field1' = 'value',
  'field2' = 'value2',
);

Then save this to a session:

$_SESSION['this_form'] = $this_form;

If you want to be really lazy you could just save the $_POST or $_GET variable in the session.

$_SESSION['my_post'] = $_POST;// or $_GET depending on your form.

And Of course, you know not to do anything with these until you validate them and do the usual inoculations.

You can retread the saved session variable later simply by calling it.

i.e.

echo $_SESSION['this_form']['field1'];

Once you are done with the session you can delete it with the unset command:

i.e.

unset $_SESSION['this_form']

There are also session functions that you can use:

PHP Session Functions

Upvotes: 10

Related Questions