Back to DFS's Workshop Page
Back to Agenda Page


Getting Data from a Form

Instead of being limited to getting data from files, PHP allows you to receive data from an HTML form as well. This capability is one of the reasons that PHP is so popular for web programming.

This programming technique requires the use of two files. The first is an HTML page which is either a static page or a dynamic one generated by a PHP script. This page transmits data back to the server where it is processed by the second file, a PHP script.

We will return to the area-of-a-triangle problem for this demonstration.

N.B. For the following code to work your web server must be running.

PHP permits the processing of data passed from an HTML form back to a script on a web server using the GET or POST methods. Below are given two pairs of files.

  1. GET & POST methods with register global variables

    Using register global variables is the least preferred technique for two reasons:

  2. GET & POST methods with $_REQUEST superglobal variables

    Using request superglobal variables is the preferred technique for two reasons:

General Notes:

GET & POST methods with register global variables

With the register_globals directive set to ON in the php.ini file, you can use the name/value pairs from an HTML form simply by prefixing the $ character to the names used in the form. Since the default setting of this directive is OFF, the following code can be used to check the setting; if the directive is set to OFF, your variables will have values of zero.

A Static HTML Page with a Form

The code below uses GET; to change to POST, replace the highlighted method specification GET with POST.

The following code should be saved in a file called triformreg.html in ~YOURID/public_html/test_php/.

<!-- triformregister.html -- calls triprocessregister.php from form,
     passing b and h -->
<html>
<head>
<title>Calculating the area of a triangle</title>
</head>
<body>
<p><a HREF=index.html>Back to DFS's PHP Page</a></p>
<hr>
<h1 align="center">Calculating the Area of a Triangle</h1>
<p>Enter the dimensions of a triangle and click on "Calculate!" to have a PHP script calculate its area.</p>
<form method="GET" action="triprocessregister.php">
<table border>
<tr>
 <td>Base</td><td><input type="text" name="b" value="" size="5"></td>
</tr>
<tr>
 <td>Height</td><td><input type="text" name="h" value="" size="5"></td>
</tr>
<tr>
 <td colspan="2" align=center><input type="submit" value="Calculate!"><input type="reset"></td>
</tr>
</table>
</form>
</body>
</html>

A PHP Script to Handle the Information from the Form

The following code should be saved in a file called triform.php in ~YOURID/public_html/test_php/. It will be executed when the user clicks on the "Calculate!" button in the form.

N.B.The following script will work only if the register_globals directive is set to "On" in the php.ini file. This technique is not the preferred one, however, because passing variables from a form in this way can lead to serious security problems. Consequently, it is recommended by the creators of PHP that this directive be left set to "Off", the out-of-the-box default.

<!-- triprocessregister.php -->
<html>
<head>
<title>Calculating the Area of a Triangle: Result</title>
</head>
<body>
<?php
// This script illustrates how to process information
// sent from a form on a web page

// The triangle dimensions $b (base) and $h (height)
// are passed from triformregister.html

echo "<h1 align=\"center\">Calculating the Area of a Triangle: Result</h1>\n";
// Print the base
echo "<p>b is $b.\n";

// Print the height
echo "<p>h is $h.\n";

// Calculate and print result
$A = $b * $h / 2;
echo "<p>The area of a triangle with a base of $b and a height of $h is $A.\n";
?>
</body>
</html>

GET & POST methods with $_REQUEST superglobal variables

If your ISP has decided to leave the register_globals directive turned OFF, you will need some other means of accessing values passed from an HTML form to your PHP script.

If the GET method has been used in the HTML form, the values can be accessed in the receiving script from the $_GET associative array; if the POST method has been used in the HTML form, the values can be accessed in the receiving script from the $_POST associative array. There are two disadvantages of using these arrays:

A third technique, using the $_REQUEST associative array, is preferable. It consolidates all GET- and POST-method names/values into a single array which does not distinguish their source.

To access the data passed to your script, you could directly reference the item from the array. E.g., to calculate the area of a triangle when the base and height have been passed using the names b and h, you could use the following line of code:

$Area = $_REQUEST['b'] * $_REQUEST['h'] / 2;

While these associative arrays are globally accessible in your script, from a programming perspective, it is probably cleaner to turn them into local variables, such as $b and $h, which could then be passed as parameters to any necessary functions.

$b = $_REQUEST['b'];
$h = $_REQUEST['h'];
...
$Area = $b * $h / 2;

This technique has the added advantage of "documenting" all variables passed to your script.

A Static HTML Page with a Form

The code below uses GET; to change to POST, replace the highlighted method specification GET with POST.

The following code should be saved in a file called triformreq.html in ~YOURID/public_html/test_php/. It is identical to the triformreg.html file given above except for the file name specifications which are here highlighted.

<!-- triformrequest.html -- calls triprocessrequest.php from form,
     passing b and h -->
<html>
<head>
<title>Calculating the area of a triangle</title>
</head>
<body>
<p><a HREF=index.html>Back to DFS's PHP Page</a></p>
<hr>
<h1 align="center">Calculating the Area of a Triangle</h1>
<p>Enter the dimensions of a triangle and click on "Calculate!" to have a PHP script calculate its area.</p>
<form method="GET" action="triprocessrequest.php">
<table border>
<tr>
 <td>Base</td><td><input type="text" name="b" value="" size="5"></td>
</tr>
<tr>
 <td>Height</td><td><input type="text" name="h" value="" size="5"></td>
</tr>
<tr>
 <td colspan="2" align=center><input type="submit" value="Calculate!"><input type="reset"></td>
</tr>
</table>
</form>
</body>
</html>

A PHP Script to Handle the Information from the Form

The following code should be saved in a file called triprocessrequest.php in ~YOURID/public_html/test_php/. It will be executed when the user clicks on the "Calculate!" button in the form.

<!-- triprocessrequest.php -->
<html>
<head>
<title>Calculating the Area of a Triangle: Result</title>
</head>
<body>
<?php
// This script illustrates how to process information
// sent from a form on a web page

// The triangle dimensions $b (base) and $h (height)
// are passed from triformrequest.html
$b = $_REQUEST['b'];
$h = $_REQUEST['h'];

echo "<h1 align=\"center\">Calculating the Area of a Triangle: Result</h1>\n";
// Print the base
echo "<p>b is $b.\n";

// Print the height
echo "<p>h is $h.\n";

// Calculate and print result
$A = $b * $h / 2;
echo "<p>The area of a triangle with a base of $b and a height of $h is $A.\n";
?>
</body>
</html>

Notes


© 2002-2007 DFStermole
Created: 22 Sept 02
Last Modified: 17 Dec 2007