Page 1 of 2
PHP Form split into many pages
Posted: Thu Oct 02, 2008 7:34 am
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
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 10:21 am
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.
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 10:31 am
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.
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 10:43 am
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

Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 10:49 am
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.
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 10:51 am
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?
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 10:54 am
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?
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 10:59 am
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.
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 11:06 am
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>
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 11:12 am
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.
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 11:17 am
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.
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 11:18 am
by VladSun
If this is the OP case then it's solved, I do agree

Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 11:23 am
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.
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 11:27 am
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.
Re: PHP Form split into many pages
Posted: Thu Oct 02, 2008 11:30 am
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.