How to Pass All of the Elements of a Form to a Second Form with PHP
Create a Useful Function for Splitting Large Forms into Multiple Steps
What happens when you want to send all of the elements of one form along to a second form?
This may seem like a strange request, but you'll find yourself in situations like this. For example, you might want to break one large form into two or three smaller forms. With each step, you need to pass along the old information so that it eventually gets to the final processing page.
Note: All html printed in this tutorial has been modified. The normal brackets [< >] have been replaced by parentheses [( )]. This is so that the article renders properly. Be sure to change this back if you copy and paste the code examples.
Using the Hidden Type of HTML (Input)
HTML comes with a tool specially designed for this purpose - the hidden type of an (input) tag.
Normally, you create an (input) tag to get user input. This information is then sent along to the processing page. If the (input) is of type text the user sees a small text box.
If the type hidden is used, the field doesn't appear on the page. The user never sees it, but it is sent along to the processing page. When the field is processed by php, the type of field doesn't matter - just the name.
So, in the first page of the form, we might have this tag...
(input type="text" name="username")
This gets passed along to the second page, but we want to pass it along again to the final processing page. One way to do that is...
(input type="hidden" name="username" value="(?php $_POST["username"] ?)")
The input is still called "username," so the final processing page can still access it through $_POST["username"].
Standardizing This in a Function
This is a great trick, but it would be cumbersome to re-type a line for every element of a form that you want to pass along.
Using a loop and some php magic, you can create a standardized function to convert every element of a form into a new hidden input to be passed along and processed.
function passAllPost($post)
{
foreach ($post as $id => $value)
echo "(input type=\"hidden\" name=\"$id\" value=\"$value\" /)";
return 0;
}
This function makes use of a php tool called foreach. This loops through an array and creates some temporary variables. In this case, $id is the name of the $_POST element and $value is the value that was held in $_POST.
This function takes $_POST as a parameter. It then goes through each element in $_POST and echoes an (input) of type hidden.
Putting It All Together
Now that you have a function, go back and put it into your form pages.
Since this function outputs the (input) tags directly, you should include this in the output of your script. It should be placed anywhere between the (form) and (/form) tags.
This is a simple but powerful function that you should keep around for your projects. You never know when you'll need to pass on all of the elements of a form. When you do, you'll be thankful that it's this easy.
Published by B. Rock
I'm a recent graduate, a newly wed, and a (no longer first year) teacher. I teach HS Social Studies in a New Jersey city. I graduated from the Rutgers Grad School of Ed in May of 2007. In July '07, I... View profile
- The Nature of the PsycheA paper that explores the ways in which we define ourselves and examines the meaning of the self throughout history, illustrating those contexts with examples from contemporary and classic film sources.
- Creationism: The Origin of Life DebateWhat is life, and who has the authority to define it. Without a clear understanding of what life constitutes, there will never be an adequate consensus on the hottest topic to date.
Group Bike Rides: The Beauty of DraftingBicycle riding with a group can be a really fun, social, and safe way to meet your fitness goals. One of the main benefits of a group of cyclists riding together is the efficien...- Elements of Socialization in Gender RolesAmong the early discoveries for the student of sociology is the idea that socialization works to shape the individual. Supporting this idea are writings by Michael A. Messner, Sharlene Hesse-Biber, and Mary Romero.
- The Invention of Lithography and Offset PrintingLithography is Greek for the words Senefelder used to describe his new method: stone printing. It took a full four years for Senefelder to perfect his process. Before his discovery, there were only two kinds of metho...
- Larry Gonick's Graphic Work The Cartoon History of the Universe
- 6400 Miles (chapters 1-4) Into the Heart of America
- What's So Important About the Declaration of Independence
- Physical Fitness: How Tough is it on the Body to Serve in the Military?
- The History of Cirque Du Soleil: Dazzling Human Circus and More
- A Modern Education Reality Check: The Vital Role of the Parent in Young Learner's...
- The Ghosts of Jim Comes to Joburg: The Search for a True Cultural Narrative in Pos...
- The "hidden" type of input passes along a value in a form without the user knowing it.
- Foreach creates a loop that cycles through every element of an array.
- This function is useful for splitting large forms into multiple steps.



