Multi Page Forms In PHP

5th May 2009 - 4 minutes read time

Multi pages forms are just as they sound, a single form spread across multiple pages. These are useful in terms of usability as it can break up an otherwise dauntingly big form into smaller chunks. It can also be useful if you want to process some of the results in order to determine what forms the user sees on later steps.

There are two ways in which it is possible to do this using PHP.

The first (and simplest) is just to cycle through the items submitted on a previous form and print them out as hidden fields. Our first page source code will look like this:

  1. <form action="form2.php" method="get">
  2. Name: <input type="text" name="name" />
  3. <br />
  4. <input type="submit" value="Proceed">
  5. </form>

On submitting the form we are taken to form2.php, which asks the user a different question and prints out the hidden fields. Because we used a get request for our first form we need to use the $_GET array.

  1. <form action="end.php" method="get">
  2. Colour: <input type="text" name="colour" />
  3. <br />
  4. <?php
  5. foreach ( $_GET as $key=>$value ) {
  6. if ( $key!="submit" ) {
  7. $value = htmlentities(stripslashes(strip_tags($value)));
  8. echo "t<input type="hidden" name="$key" value="$value">\n";
  9. }
  10. }
  11. ?>
  12. <input type="submit" value="Proceed">
  13. </form>

This same code can be used on the different pages of the form. This method is fine, and works quite well, but it doesn't account for users going back through the form and resubmitting a previous item. The $_GET array will only contain information about the previous forms.

To make this more user friendly, and robust, we need to employ a session to store our form values as the user goes through the form. Using sessions means that the user can cycle back and forward through the forms with no ill effect on the form data. The following code will take the input of the previous form and save it as a PHP session.

  1. session_start();
  2. foreach ( $_GET as $key=>$value ) {
  3. if ( $key!="submit" ) {
  4. $value = htmlentities(stripslashes(strip_tags($value)));
  5. $_SESSION[$key] = $value;
  6. }
  7. }

This must be included on every page of our multi page form. If it is not then the data will simply not be saved for that step. Your users can now move back and forward through the forms, saving the information as they go. You need to supply a back button for this to work as using the browser back and forward buttons will also not save the data.

Finally, when testing this code I found that using GET rather than POST was beneficial in terms of usability. This is mainly because if you use POST requests and the user clicks the back on their browser they will be asked if they want to resubmit the information for that form.

Does anyone else have any ideas about how to do this? If so then post a comment and suggest it.


script works perferkt. just not when the form contains array's. so I get this error message: Warning: strip_tags() expects parameter 1 to be string, array given in /customers/ How do i go about that?

Anonymous (Wed, 07/14/2010 - 13:02)

Use is_array() to check for the presence of an array in your form. If it is one then you are probably looking at a select form or something so there is no need to use strip_tags() on it. Of course it might be prudent to run them through a separate loop and use strip_tags() on them, just in case.

Add new comment

The content of this field is kept private and will not be shown publicly.