PHP Form split into many pages

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

Rabi
Forum Newbie
Posts: 8
Joined: Thu Oct 02, 2008 7:16 am

PHP Form split into many pages

Post by Rabi »

Hi There,

I am relatively new to PHP and recently I have been tasked develop a web based form that allows users to input quote details like the one we see for insurance quotes.

So once user credentials have been established First part of the form will be displayed and user must fill required fields and then click 'NEXT' page. One the next page there are more fields and this way entire from is split into 4 pages! User should have option to go 'PREV' (from 2nd, 3rd and 4th page) or 'NEXT' (1st,2nd and 3rd page) page. Here I am not sure how to achieve this.

Any help or guidance will be greatly appreciated
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: PHP Form split into many pages

Post by Paul Arnold »

Hi.

I'd suggest using sessions for this.

For example, once you submit a form,

Code: Select all

 
session_start();
$_SESSION['firstname'] = $_POST['firstname'];
 
Then for your input boxes put:

Code: Select all

 
<input type="text" name="firstname" value="<?PHP if(isset($_SESSION['firstname'])) { echo $_SESSION['firstname']; } ?>" />
 
Do this for all your input boxes.
This is a very basic example and you should of course validate all of your input.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP Form split into many pages

Post by VladSun »

I would suggest you to use a hidden form field - state.
This field will have a value of a serialized array.
On every step of your wizard, you add submitted values as elements of this array, serialiaze it again and include it as a hidden field.
You may encrypt it or HMAC it in order to have some security.

This way you permit your users to use back and forward buttons, which is not so with the SESSSION apporach.
There are 10 types of people in this world, those who understand binary and those who don't
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: PHP Form split into many pages

Post by Paul Arnold »

VladSun wrote:I would suggest you to use a hidden form field - state.
This field will have a value of a serialized array.
On every step of your wizard, you add submitted values as elements of this array, serialiaze it again and include it as a hidden field.
You may encrypt it or HMAC it in order to have some security.

This way you permit your users to use back and forward buttons, which is not so with the SESSSION apporach.
Really?

I would've thought the sessions approach would be fine with back and forward buttons :?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP Form split into many pages

Post by VladSun »

OK, my mistake - it will work for bac/forward :)
But it will not work for multiple instances of the same page with different data submitted by a single user.
There are 10 types of people in this world, those who understand binary and those who don't
Rabi
Forum Newbie
Posts: 8
Joined: Thu Oct 02, 2008 7:16 am

Re: PHP Form split into many pages

Post by Rabi »

VladSun wrote:I would suggest you to use a hidden form field - state.
This field will have a value of a serialized array.
On every step of your wizard, you add submitted values as elements of this array, serialiaze it again and include it as a hidden field.
You may encrypt it or HMAC it in order to have some security.

This way you permit your users to use back and forward buttons, which is not so with the SESSSION apporach.

I hope I am not asking too much but can you give me an example please?
Rabi
Forum Newbie
Posts: 8
Joined: Thu Oct 02, 2008 7:16 am

Re: PHP Form split into many pages

Post by Rabi »

Paul Arnold wrote:Hi.

I'd suggest using sessions for this.

For example, once you submit a form,

Code: Select all

 
session_start();
$_SESSION['firstname'] = $_POST['firstname'];
 
Then for your input boxes put:

Code: Select all

 
<input type="text" name="firstname" value="<?PHP if(isset($_SESSION['firstname'])) { echo $_SESSION['firstname']; } ?>" />
 
Do this for all your input boxes.
This is a very basic example and you should of course validate all of your input.
I got you!! One more thing. on one of the pages there is going to be an option to attach a file to the form. How will that be handled? Can you provide some guidance on this please?
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: PHP Form split into many pages

Post by Paul Arnold »

Make sure your form tag has the enctype set to multipart/form-data..

Code: Select all

<form method="post" enctype="multipart/formdata">
You'll need a file input box

Code: Select all

<input type="file" name="file1" />
And then you're going to need a file upload script.

There's a simple little tutorial here:

http://php.about.com/od/advancedphp/ss/ ... upload.htm

Edit: Just to mention security should be paramount when allowing people to upload to your server. Do a little research and gain some understanding on how to achieve this.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP Form split into many pages

Post by VladSun »

Rabi wrote:I hope I am not asking too much but can you give me an example please?
What Paul Arnold suggested is to "expand" your form on every step with form fields containing the values of previously submitted form. Now I an see that in fact using SESSION is really not needed in his approach...

I' suggesting the same thing but instead of using multiple fields, I use only one field which contains all the values in a serialized form.

Code: Select all

 
<?php 
if (!$_POST['state'])
    $_POST['state'] = array();
else
    $_POST['state'] = unserialize($_POST['state']);
    
$_POST['state'] = array_merge(array($_POST['input1'], $_POST['input2']), $_POST['state'])
 
......
 
?>
 
 
<form method="post">
    
    <input type="text" name="input3" value="" />    
    <input type="text" name="input4" value="" />
    ....
    <input type="hidden" name="state" value="<?php echo serialize($_POST['state'])?>">  
    <input type="submit" value="ok">
</form>
Last edited by VladSun on Thu Oct 02, 2008 11:13 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP Form split into many pages

Post by VladSun »

@Paul Arnold
I can't agree this approach for handling file uploads is the right one.
Imagine this as a wizard - every step has 3 choices:
Next, Back, Cancel

The "Cancel" one can be simply closing the browser.

We have to think of an approach which will not submit for processing any data until the final step is confirmed.
There are 10 types of people in this world, those who understand binary and those who don't
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: PHP Form split into many pages

Post by Paul Arnold »

I was assuming the file upload thing would be applied after the submission of all of these details to be honest.

I was just trying to give the OP a basic introduction into how to handle file uploads.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP Form split into many pages

Post by VladSun »

If this is the OP case then it's solved, I do agree ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP Form split into many pages

Post by VladSun »

If Rabi is going to use your approach, he still needs to make some security checks.
I imagine that the wizard will not go a step forward until the new data submitted is properly validated.
But the "old" data can be tempered by the user, so we need to validate the "old" data on every step.

With the "state" approach it is much easier because it's a single field.
I mean: encryption, HMAC or even data compression.
There are 10 types of people in this world, those who understand binary and those who don't
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: PHP Form split into many pages

Post by Paul Arnold »

Absolutely.

My assumption would be that the data gets validated at every step, regardless of how it is stored.
At the end of the 'details' process the data would need to be 'confirmed' by the user, this is the last stage at which the data can be amended, and THEN they are given the opportunity to upload a file.
Once the file is uploaded, the location is stored along with all of the previous data entered.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP Form split into many pages

Post by VladSun »

:)
The last thing we have both to agree with is that some of the values submitted in a step can strongly depend on a values submitted in a previous step (that's is usually so for wizards). So we can't really (or it will be too dificult to) validate all of the data in the final step. We should keep track on the validity of the data on every step.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply