#native_company# #native_desc#
#native_cta#

Pass Values Across Multiple Forms Using PHP

By Quincy Bailey
on January 22, 2001

Submitted By: Quincy Bailey

Date: 1/1/01 19:09

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:

<form action="process_form.php3">

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:

<input type="hidden" value="somevalue" name="somename">

The hidden input fields will store the names and values of form elements from previous forms in the current form the user is on.

We’ll also use a little PHP, which will generate the hidden input fields for us on-the-fly.

Using the POST Method, hidden input fields, and PHP, we can easily carry user input across multiple forms. It just takes a few lines of code. Here’s the actual script:

<?php

while (list($name, $value) = each($HTTP_POST_VARS))
  {
   echo "<input type="hidden" value="$value" name="$name">n"; 
  }

?>

Here’s what we find when we break it down:


while (list($name, $value) = each($HTTP_POST_VARS))

The code above is a loop that accesses the elements stored in $HTTP_POST_VARS.

  {
   echo "<input type="hidden" value="$value" name="$name">n"; 
  }

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

<form></form>

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.

<?php

/* ---------------------
    Declare Variable(s) 
   --------------------- */

$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"; 
  }

?>

Until next time, happy coding!