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

B. Rock
Forms are powerful tools for your php scripts. They allow you to take user generated input and do something special with it.

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

To comment, please sign in to your Yahoo! account, or sign up for a new account.