Multiple Page Forms

So you have this long, involved form. Rather than present the user with a myriad of inputs on a single page, you want to break this up into separate form pages. So now you're asking, "How do you make multiple page forms?" I'm glad you asked. It's easy!

Here's one easy way. Consider the following small bit of code, which assumes that your form is submitted via the POST method:

  foreach($_POST as $key=>$value){
    if ($key!="submit"){
      echo "\t<input type=\"hidden\" name=\"$key\" value=\"$value\">\n";

What's that bit of code do? If you were to place the above code between your opening <form> tag and the closing </form> tag, it will look at all values received from the prior page, strips out any HTML tags, replaces any character entities with their appropriate code and writes them to hidden inputs in the current form. It will skip a value of "submit" so it won't duplicate your submit button. If you name your submit button something other than "submit", you should change that bit in the code.

Here is a simple form that you can try out. First, look at the source code of this page. In particular, look at the <form> and confirm that there are no hidden inputs. The form simply submits back to this page. Type something in the box and click the submit button. It doesn't matter what you type, the value will simply be plugged into the resultant page as a hidden input in the form.


Now, after submitting the form, look at the source code again. Now you will notice that the value you typed in the input box is included as a hidden field in the form. Okay, let me insert a small disclaimer here. This is only an example to demonstrate a technique. If you look at the source code after submitting the form, you will notice that you have two inputs with the same name, one hidden and one visible. If you had submitted the form to a different page, with different inputs, that would not happen.