CSC 325 | Grinnell College | Fall, 2008 |
Databases and Web Application Design | ||
This laboratory exercise presents several basic elements of dynamic Web pages and PHP programming.
The Web page sample.html from the previous lab and most lab documents for most courses are static. That is, each document was created once with all information included at that time. Each document is static and does not adapt to user input.
In contrast, a dynamic document is produced by a program that can receive input from a user and modify the Web page based on that input. One approach for achieving this dynamic processing is to embed programming statements within a Web page. The PHP programming language provides one mechanism for this processing.
With PHP scripting within a Web page, the sequence of events for Web interaction includes a step during which a Web server executes a PHP program:
For HTML/PHP scripts to be helpful, of course, we need to be able to transmit data from a browser to the server for processing. In summary, this transmission can be accomplished in two main ways:
We now discuss at each of these approaches in some detail.
A query string is a string of characters that are appended to a URL following a question mark. For example, consider the URL:
Here the number 10 has been added to a URL; a question mark separates the URL's address from data.
The following HTML/PHP script retrieves this number, intreprets the number as a value of inches, computes the corresponding number of centimeters, and packages this information in a Web page.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Inch Converter</title> </head> <body> <?php ini_set("register_globals", "on"); $inches = getenv("query_string"); $centi = $inches * 2.53998; ?> <H1>Inch Converter</h1> <center> <?php echo $inches; ?> inches = <?php echo $centi; ?> centimeters </center> </body> </html>
We now outline how this script works; see the textbook for many additional details. All of the following notes relate to the above program listing.
<?php // this is a PHP comment -- put PHP code here as well ?>
ini_set("register_globals", "on");
$inches = getenv("query_string");
Copy this HTML/PHP script, located in ~walker/public_html/courses/325.fa08/labs/inches.php, to your public_html directory, and set its permissions to allow Web access.
Edit the program to comment the global registration out:
// ini_set("register_globals", "on");
Describe what happens.
What happens if you misspell the second time $inches? (Keep the first variable as is, but change the spelling of the second.) Remember this experiment in your future coding, as there is not a compiler that will report variations of spelling!
While query strings provide a useful mechanism of transferring data, typing data into a URL can be cumbersome. Further, if several values are needed in a program, some clarity is needed regarding which value is which. Forms provide some solutions to these problems.
For example, consider the following framework that asks a user to provide values for feet and inches:
Enter a length in feet and inches below:
Enter values for feet and inches in this form, and click "submit"
To explain how this form and the corresponding HTML/PHP script work, we look at the form and script in turn. This form uses the following HTML segment:
<form action="feet-inches-1.php" method="GET"> Feet: <input type="text" name="feet" size=12> Inches: <input type="text" name="inches" size=12> <p> <input type="submit" value="Submit"> </form>
This form has the following features:
Turning to the HTML/PHP script, the header of the new program is similar to the earlier example. The body of the page, however, has some revision:
<?php $inches = $_GET["inches"]; $feet = $_GET["feet"]; $centi = ($feet * 12 + $inches) * 2.53998; ?> <H1>Foot-Inch Converter</h1> <center> <?php echo $feet; ?> feet, <?php echo $inches; ?> inches = <?php echo $centi; ?> centimeters </center>
This code is quite similar to the earlier code as well, except that the retrieval of data is simplified. In particular, the _GET variable provides access to each field in the form. Thus, $_GET["feet"] retrieves the value entered for feet in the form, and $_GET["inches"] retrieves the value entered for inches.
To conclude this example, three additional notes may be useful:
ini_set("register_globals", "on");often is not needed — this depends upon default settings on the server. If you are working in an environment and cannot retrieve data, try adding this line at the start of your HTML/PHP script.
$inches = $HTTP_GET_VARS["inches"]; $feet = $HTTP_GET_VARS["feet"];
Although useful, the GET method of data transmission has at least two difficulties. In addition to the limit on the size of the query string, all data are easily visible in the URL. Both of these restrictions are resolved with the POST method. In this approach, both the form and the HTML/PHP program look almost the same as before, with the word GET replaced by POST. The corresponding form (calling feet-inches-2.php) is:
Similarly, the program retrieves data from the form with these lines:
$inches = $_POST["inches"]; $feet = $_POST["feet"];
Use the form to enter values for feet and inches, and click submit.
Write your own form and program to convert gallons and quarts to liters (note that 1 liter = 1.056710 quarts). Write one version that uses the GET method and another that uses the POST method.
Once you know how to retrieve data from a Web page, you can use all of your past programming experience to write applications.
The PHP language defines if statements in much the same way found in C and Java. Here are three simple examples:
if ( $x > 0 ) { echo "the number is positive"; } if ( $x > $y ) echo $x . " is larger than " . $y; else echo $x . " is less than or equal to " . $y; if ( $x > $y ) { echo $x . " is larger than " . $y; } else { if ( $x == $y ) { echo $x . " is equal to " . $y; } else { echo $x . " is less than " . $y; } }
Write a form that asks for three values, the sides of a triangle.
Then write an HTML/PHP script that retrieves these values and prints one of the following conclusions:
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/325.fa08/lab-dynamic-pages.shtml
created 10 August 2008 last revised 22 September 2008 |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |