The ability to easily manipulate the information that users submit via HTML forms has been one of the advantages of PHP. In fact, PHP version 4.1 adds several new ways to access this information and effectively removes one of the most commonly used methods from previous releases. This article explores different ways to use the information submitted on an HTML form and uses both earlier versions of PHP and newer versions. This article begins with a study of a single value, and then constructs a page that provides general access to 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 through this article, you'll 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
<title>Tour information</title>
Mission Information
Without specifying a method, the form uses the default method GET, which the browser uses to append 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
Old-fashioned: accessing global variables
The code shown in Listing 2 handles the form values as global variables:
Listing 2. Form values as global variables
echo "ship =". $ship;
echo "
";
echo "tripdate =". $tripdate;
echo "
";
echo "exploration =". $exploration;
echo "
";
echo "contact =". $contact;
?>
The generated Web page displays the submitted values:
ship = Midnight Runner
Tripdate = 12-15-2433
Exploration = yes
Contact =
(as you'll see later, the contact has no value, because that box is not selected).