The ability to easily manipulate the information that users submit through HTML forms has been one of the advantages of PHP. In fact, PHP version 4.1 adds several new ways to access the information and effectively removes one of the most commonly used methods in previous versions. This article studied different ways to use the information submitted on an HTML form and used both earlier versions of PHP and newer versions. This article starts with a study of a single value and then builds a page that can generally access any available form values.
Note: This article assumes that you have access to a Web server running PHP version 3.0 or later. You need to have a basic understanding of PHP itself and the creation of HTML forms.
HTML form
As you read this article, you will see how different types of HTML form elements provide information that PHP can access. For this example, I used a simple information form that consists of two text fields, two check boxes, and a selection box that allows multiple items:
Listing 1. HTML form<body>
<form action= "formaction.php" >
<table width= "100%" >
<tr><td>ship name:</td><td><input type= "text" name= "ship"/></td></tr>
<tr><td>trip date:</td><td><input type= "text" name= "Tripdate"/></td></tr >
<TR><TD colspan= "2" >mission goals:</td></tr>
<tr>
<td><input type= "checkbox" Name= "Exploration" value= "yes"/>
Exploration</td>
<td><input type= "checkbox" name= "Contact" value= "yes"/>
Contact</td>
</tr>
<tr>
<TD valign= "Top" >crew species: </td>
<td>
<select name= "Crew" multiple= "multiple" >
<option value= "Xebrax" >Xebrax</option>
<option value= "Snertal" >Snertal</option>
<option value= "Gosny" >Gosny</option>
</select>
</td>
</tr>
<TR><TD colspan= "2" align= "center" ><input type= "Submit"/></td></tr>
</table>
</form>
</body>
In the absence of a method specified, the form uses the default method get, which the browser uses to attach the form value to the URL, as follows:
http://www.vanguardreport.com/formaction.php?
ship=Midnight+Runner&tripdate=12-15-2433&exploration=yes&crew=snertal&crew=gosny
Figure 1 shows the form itself.
Figure 1. HTML form
The old fashioned: accessing global variables
The code shown in Listing 2 handles the form value as a global variable:
Listing 2. Form values as global variables<?php
echo "Ship = ".$ship;
echo "<br />";
echo "Tripdate = ".$tripdate;
echo "<br />";
echo "Exploration = ".$exploration;
echo "<br />";
echo "Contact = ".$contact;
?>