Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - How to save the form data temporary

Tags:

php

session

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.

like image 674
q0987 Avatar asked Dec 08 '22 01:12

q0987


1 Answers

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

like image 147
dkinzer Avatar answered Dec 25 '22 15:12

dkinzer