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?
How to Make a Multi-page Form Using PHP - Tutorial Collection said this on June 4th, 2009 at 9:22 pm
[...] 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. [...]
Clubnite said this on November 2nd, 2009 at 7:54 am
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!?
Simon said this on March 25th, 2010 at 11:08 pm
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!
Simon said this on March 26th, 2010 at 10:58 am
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.
otaesteojg said this on May 5th, 2010 at 4:51 pm
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
Guilherme Borges said this on July 8th, 2010 at 6:42 am
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!
Guilherme Borges said this on July 8th, 2010 at 6:42 am
Page 1:
page 1
Page 2
Page: Page_choice.php:
<?
Guilherme Borges said this on July 8th, 2010 at 7:01 am
I am not able to send the case here … could someone pass me some email so that I could pass the idea.
Desiree Sparger said this on June 19th, 2011 at 2:20 pm
Your pages had some errors although I browsed. I don’t know if it is one thing incorrect or simply my browser acting up but I believed I let you know.
Jinny Warren said this on July 3rd, 2011 at 12:52 am
Its like you read my thoughts! You seem to realize so much about it, enjoy you wrote the book in it or something. I consider which you might do with most pics to hard drive the concept house a bit, but other than that, it is great website. A excellent read. Ill surely be again.
geoge said this on July 28th, 2011 at 10:28 am
gFwuZM http://fnYwlOpd2n9t4Vx6A3lbk.com
Dhnzjguy said this on August 29th, 2011 at 2:57 pm
What’s the current interest rate for personal loans? bbs dark lolita yo :-]
Zwwijplb said this on August 29th, 2011 at 2:57 pm
The National Gallery free lolitas preteen sites 4052
Xnhyztlj said this on August 29th, 2011 at 2:58 pm
Incorrect PIN hot nude lolita pictures %[[
Wpfnsjhx said this on November 10th, 2011 at 7:09 pm
Will I have to work shifts? Nn Model Sierra
564034
Cqvzivhz said this on November 10th, 2011 at 7:10 pm
On another call Nn Modeling
8[[
Xcsstved said this on November 10th, 2011 at 7:10 pm
How many weeks’ holiday a year are there? Nn Model Sierra
:-]