HTML forms -- what would the web be without them? After you complete and submit a form, the input is usually sent to some script that processes it. The script may send the input via e-mail or insert it into a database.
Creating this type of functionality is usually a snap. First, you create an HTML form with whatever fields you want the user to fill out. Then you create a script that does something with the user input it receives from the form. The final step is to make the form's ACTION attribute point to the script, like this:
That's it. User input is collected via the form and processed by the script. Piece of cake, right?
But what if the user has to complete more than one form?
The tricky part is transferring the user's input from one form to the next until it reaches the script.
We must somehow store the user's input from previous forms as fields in the current form the user is on. That way all the answers will be carried over when the user submits the form.
To help accomplish this task, we'll use hidden input fields. If you've played around with forms before you're probably already familiar with them. They look like this:
All we're doing here is getting the name and value of an element and using them to construct a hidden input field. This segment of code serves as a template for generating the hidden input fields.
To sum it up, the script will loop through each element stored in $HTTP_POST_VARS and generate a hidden input field for it.
Just make sure you do the following:
1. Copy and paste the script somewhere between the
tags in whatever form(s) you want.
2. Use POST in the form's METHOD attribute. If you don't specify anything, it will use GET and the script won't work.
The following is the uncut version of the script, with comments and all. Feel free to modify the script to suit your needs.
$name; // Represents the name of the elements in $HTTP_POST_VARS
$value; // Represents the value of the elements in $HTTP_POST_VARS
// Do while there is an element in $HTTP_POST_VARS...
while (list($name, $value) = each($HTTP_POST_VARS))
/* ...get the name and value of the element and use it to construct a hidden input field.
Then go back to the beginning of the loop and do the same thing with the next element. */
echo "<input type=\"hidden\" value=\"$value\" name=\"$name\">\n";