How to Pass All Elements of a Form to the End in a Multi-Page Form

Sometimes forms get long, unsightly, and intimidating. It’d be great if you could cut them up into three or four parts – with a few fields on each page.

In order to do that, you need to figure out some way of storing or passing along the information from the first pages. I’ve heard a bunch of crazy ideas – from storing the information in session variables to writing each part to the database.

The easiest thing to do is attach a very brief php function to the bottom of each page. It’ll do all the work for you and continue to post every item the user has entered.

To start with, let’s create a hypothetical page one of a form. This will give us something to work with for the rest of this tutorial.

<form action="script.php" method="post">
  <input type="text" name="firstname" />
  <input type="text" name="lastname" />
  <input type="submit" name="poneSubmit" value="Continue" />
</form>

Here we’ve got three form elements – although only two of them really count. When we get to the second page, we need to make sure that “firstname” and “lastname” are passed along so that the final processing page can store the data appropriately.

When the second page (“script.php”) loads, these values will be stored in $_POST['firstname'] and $_POST['secondname'].

Every value in a form that is submitted with method="post" ends up in the superglobal array $_POST with a key equal to its name attribute in the form.

In order to continue to pass these values along and make them accessible through the $_POST superglobal on other pages, we can use a simple foreach loop. With each iteration of the loop, we’ll create a new <input type="hidden" /> to store the information.

foreach ($_POST as $key => $val) {
  echo '<input type="hidden" name="' . $key . 
    '" value="' . $val . '" />' . "\r\n";
}

This will cycle through the $_POST array and create all of the hidden elements for us. So we’d end up with this being added to our next page in the form.

<input type='hidden' name='firstname' value='Bob' />
<input type='hidden' name='lastname' value='Stevenson' />
<input type='hidden' name='poneSubmit' value='Continue' />

All you need to do is add that snippet of php to the end of your form on each subsequent(after page one), and all of the values will end up in the $_POST array at the end. [Note: Make sure this is inside the form tags, or else it won't get passed along.]

If you want to take it one step further, you can standardize this into a neat little function. Then include this function in your common includes file, and you can use it on any page with a simple function call.

function passArray($array) {
  foreach ($_POST as $key => $val) {
    echo '<input type="hidden" name="' . $key . 
      '" value="' . $val . '" />' . "\r\n";
  }
}

That’s all there is to it! Go write up some forms.

[Note: If you're wondering why the "\r\n" is there, it's just to make the source code look nicer. It adds a newline so that your source code is nice and spread out. Otherwise, every element our function adds would be on the same line - making the source code pretty illegible.]

Update. It’s been pointed out to me that there’s a possible security hole in this method. No big deal. This article that revisits multi-page forms patches the hole up nice and snug. I’d suggest you take a look at it.

Bookmark and Share:
  • Digg
  • Furl
  • del.icio.us
  • StumbleUpon
  • MisterWong
  • DZone
  • Technorati

Tags: , , , , ,

13 Comments to “How to Pass All Elements of a Form to the End in a Multi-Page Form”

  1. How to Create Multi-Page Forms in PHP, Revisited | Web Cash said this on

    [...] A couple weeks ago, I wrote a short article about how to create a multi-page form. [...]

  2. Chris said this on

    Is it as easy as including a include.php with that snippet on each of the form pages, and thats that?

  3. Walkere said this on

    Yup, that’s all there is to this method. Add that snippet inside your form, and you’ll find all of the elements are passed along the final processing page as if they came from a single-page form.

    You may want to look at the related article “How to Create a Multipage Form in PHP, Revisited.” It was pointed out to me that there was a possible security problem with the original method, and the next article patches up the hole nice and snug.

  4. Michael Novello said this on

    Yay! But what about this: How do you redirect a user to the next page, while still using POST variables? In other words, make “action” the same filename as the current one, so you can verify the data, and then “redirect” the user to the next page while still remembering all of the data, and not using GET?

  5. Femi said this on

    Great article,

    I will like to add a save function, back functionality, can I do this using the php above?

  6. How to Make a Multi-page Form Using PHP - Tutorial Collection said this on

    [...] View Tutorial No Comment var addthis_pub=”izwan00″; BOOKMARK This entry was posted on Friday, June 5th, 2009 at 7:55 am and is filed under Php Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]

  7. Clubnite said this on

    what about the security aspect? hidden forms are available for manipulation using firefox’ web developer toolbar which is able to show all forms including hidden fields!?

  8. Simon said this on

    A couple really dumb questions:

    1: As Michael Novello said, how does the form know to send the user to the next page instead of performing the submit php script, which would, I think, e-mail the form? Where do I put the url for page 2 of the form?

    2: (remember, these are dumb questions OK?) Where does

    foreach ($_POST as $key => $val) {
    echo ” . “\r\n”;
    }

    go? Dreamweaver thinks it’s text, so not directly in the form apparently.

    Thanks!

  9. Simon said this on

    Btw, I found an easy java solution to the multi-page form challenge that also seems to work well, for anyone who is interested. http://www.devx.com/webdev/Article/10483/1763/page/2.

  10. otaesteojg said this on

    Trazodone Trazodone and cod saturday delivery interrupted Xanax cheap overnight xanax behalf Provera Provera cheap no prescription bervariasi Levitra levitra with free dr consultation Patricio Diflucan buy Diflucan order cod Sumner Topamax Topamax no rx needed Probable Strattera lowest prices for Strattera online 10000 Tramadol tramadol cod fedex mdotedu Carisoprodol carisoprodol cod orders Joystick Xanax xanax cod fedex emails Soma soma online cheap miracle Amitriptyline where can i buy Amitriptyline online tertentu Codeine cheap online order Codeine storms Tramadol pharmacy tramadol Conversations Meridia cod online meridia Hackers tramadol pill price tramadol MABPBF Valtrex buy no prescription Valtrex DescriptionA Phentermine order phentermine online fthriftbigpond

  11. Guilherme Borges said this on

    Hello Everyone, (translation via google translator)
    I am a php novice user and I’m certainly a cruel, how do I
    redirect fields of a first page from the menu list? That is if he chooses the first option will be directed to a page and most all fields of this page and if he chooses the second option will be directed to page 2 and more all fields on this page? Here’s my script … what is missing to work ok?

    Page 1:

    page 1
    Page 2

    Page: Page_choice.php:

    $ val) (
    echo ”.” \ R \ n “;
    )

    if ($ _POST ['tipo_responsavel'] == “excursion”)
    (
    (Cad_excursao.php “);
    )
    if ($ _POST ['tipo_responsavel'] == “responsible”)
    (
    (Cad_2.php “);
    )

    Hope someone can clarify my ideas. Thank you for everything!

  12. Guilherme Borges said this on

    Page 1:

    page 1
    Page 2

    Page: Page_choice.php:

    <?

  13. Guilherme Borges said this on

    I am not able to send the case here … could someone pass me some email so that I could pass the idea.

Leave a Reply