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.







How to Create Multi-Page Forms in PHP, Revisited | Web Cash said this on February 8th, 2008 at 7:59 pm
[…] A couple weeks ago, I wrote a short article about how to create a multi-page form. […]
Chris said this on February 22nd, 2008 at 3:45 am
Is it as easy as including a include.php with that snippet on each of the form pages, and thats that?
Walkere said this on February 23rd, 2008 at 10:17 am
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.
Michael Novello said this on March 5th, 2008 at 8:54 pm
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?
Femi said this on March 26th, 2008 at 2:05 pm
Great article,
I will like to add a save function, back functionality, can I do this using the php above?