How to save data for multi-page forms?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

How to save data for multi-page forms?

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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;
 
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

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

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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.
Post Reply