Page 1 of 1

How to save data for multi-page forms?

Posted: Mon Apr 27, 2009 12:05 am
by Mr Tech
What is the best way to save data from a multi-page form? Can I create a session and an array? E.g:

Code: Select all

$_SESSION['form_data'] = array();
 
$_SESSION['form_data']['page_1'] = array();
$_SESSION['form_data']['page_2'] = array();
 
$_SESSION['form_data']['page_1']['field_1'] = $_POST['field_1'];
$_SESSION['form_data']['page_1']['field_2'] = $_POST['field_2'];
$_SESSION['form_data']['page_1']['field_3'] = $_POST['field_2'];
 
$_SESSION['form_data']['page_2']['field_1'] = $_POST['field_1'];
$_SESSION['form_data']['page_1']['field_2'] = $_POST['field_2'];
Or is it better to store it in a MySQL Database? Or what other way would you recommend?

Re: How to save data for multi-page forms?

Posted: Mon Apr 27, 2009 12:11 am
by Benjamin
Session or Database depending on the application. You can clean that up a little though.

Code: Select all

 
$data = array(
    'field_one' => 'value',
    'field_two' => 'value',
    # etc
);
 
$_SESSION['page_data']['page_two'] = $data;
 

Re: How to save data for multi-page forms?

Posted: Mon Apr 27, 2009 12:17 am
by Mr Tech
What do you mean by 'depends on the application'? Are you just referring to whether the application uses a database or not? In this case it doesn't.

Do sessions have a limit capacity of how much data can be stored in them or are they practically unlimited?

Re: How to save data for multi-page forms?

Posted: Mon Apr 27, 2009 12:30 am
by Benjamin
Say you are making a user registration form, and you only want to save all the data if the user fills out all of the pages. In that case I would use SESSIONS. If you want to store the data, regardless of whether the user fills out all the pages, then I would go ahead and put it in the database right away.

In the latter case this can complicate things a bit if the user goes back and changes responses, in which case you would need to update the record rather than insert another one, but if the user stops half way through, you'll at least have some of the data stored.

As far as the amount of data that you can store in a SESSION, I'm not sure what the limit is, if there even is one. Any uploaded files are saved in the /tmp directory, so the SESSION data would only contain the path to that data. So with that said, I would not be concerned about any limits as far as SESSIONS go.