The ability to easily operate information submitted by users through HTML forms has always been one of the advantages of PHP. In fact, PHP version 4.1 adds several new methods to access this information and effectively removes the most commonly used methods in previous versions. This article studies different methods of submitting information using HTML forms, and uses earlier PHP versions and newer versions. This article begins with studying a single value, and then builds a page that can access any available form value in general.
Note: This document assumes that you have access to Web servers running PHP version 3.0 or later. You need to have a basic understanding of PHP and HTML forms.
HTML form
When reading this article, you will see how different types of HTML form elements provide PHP accessible information. For this example, I used a simple information form consisting of two text fields, two check boxes, and one option box that allows multiple numbers:
Listing 1. HTML form
Tour Information
Mission Information
If no method is specified, the form uses the default GET method, and the browser uses it to append the form value to the URL, as shown below:
Http://www.vanguardreport.com/formaction.php?
Ship = Midnight + Runner & tripdate = 12-15-2433 & authentication = yes & crew = snertal & crew = gosny
Figure 1 shows the form itself.
Figure 1. HTML form
Old method: Access global variables
The code shown in Listing 2 treats the form value as a global variable:
Listing 2. Form values as global variables
Echo "Ship =". $ ship;
Echo"
";
Echo "Tripdate =". $ tripdate;
Echo"
";
Echo "annotation =". $ annotation;
Echo"
";
Echo "Contact =". $ contact;
?>
The submitted value is displayed on the generated Web page:
Ship = Midnight Runner
Tripdate = 12-15-2433
Token = yes
Contact =
(As you will see later, there is no Contact value because the box is not selected ).