Hidden Form Fields

There are times when you want to take information contained
in a Web page, and pass it to another Web page without requiring any input from
the user. There's another setting for the <input>
control that enables you to pass information in a field just as if it were a
text box but keeping the control and its contents hidden. This is known as a
hidden form field (or hidden control).

Hidden form fields come into play in a slightly different manner
than the controls already demonstrated. They're probably more useful on PHP
pages that contain forms because you can use them to send information contained
within PHP variables. Here's a typical hidden form field on a form:

<input type="hidden" name="Hidden1" value="Secret Message">

There's no screenshot of this because the control wouldn't appear
on the page. Any form that submitted it though would have a variable called
$Hidden1 that contains the text Secret Message. To use the hidden form field in a PHP page,
you can write the whole HTML form in echo() statements,
transferring the contents of PHP variables via HTML controls as shown here:

This entire HTML form is written in PHP statements and it enables
you to create a variable called $Hidden2 and transfer
the contents of $Message1 into it. Of course, this is
not the only way to keep data available across form submissions
or page requests; cookies, sessions, and so on are discussed later on in this
chapter.

Now for an example that takes the contents of a <select> list box and displays the user's choice on the
next page. The preceding process is used to write the HTML form in PHP echo statements as well.

How it Works

Once you get over the process of creating the HTML form in
echo statements rather than straight HTML code, this is really very
straightforward. The main difference is that you can leave out the apostrophes in the variable name in the array. Three variables are
created to form the basis of the <select> list
box:

They are respectively $Message1, $Message2, and $Message3. Next, the
HTML form is created using echo statements. Absolutely
nothing differs from a normal HTML form, except that when you want to use
quotation marks in the HTML, you have to use single quotation marks and not
double ones or you'll break the echo statement. The
first line just tells you to send the form contents to hidden2.php via the GET method:

echo "<form method='GET' action='hidden2.php'>";

Some explanatory text is displayed and then the <select> list box is started:

echo "Which of the following would win in a shootout?";
echo "<select name='ListBox'>";

It gets three options, the contents of the variables $Message1, $Message2, and $Message3,
respectively.

This is useful because normally the contents of the entire list
box aren't transferred across. Only the option selected by the user is passed
over to the next PHP page. However, sometimes you want to have all of the list
box contents available in your PHP page. This is one effective method for
transferring this type of information.

The last lines just display the contents of selection made by the
user.

echo "<br>You selected:<br>";
echo "$_GET[ListBox]";

Hidden form fields are used to perform this type of task
later in this book.