CSC 325 Grinnell College Fall, 2008
Databases and Web Application Design

Laboratory Exercise on Dynamic Web Pages

This laboratory exercise presents several basic elements of dynamic Web pages and PHP programming.

Static and Dynamic Web Documents

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:

  1. Within a Web browser, you type a URL.
  2. Your browser sends a request to the server for that address.
  3. The server finds the file on a disk drive.
  4. The server retrieves the file from the disk and recognized the file as an HTML/PHP script.
  5. The server runs the PHP program within a Web page and produces a complete HTML document.
  6. The server sends the newly-produced HTML document back to your browser.
  7. Your browser interprets the file and displays it on your screen.
This sequence of events is illustrated in the following diagram.

Client-server interaction for the World-Wide Web - 2

Communicating Data to a Web Server

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.

Query Strings

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">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Inch Converter</title>
    ini_set("register_globals", "on");
    $inches = getenv("query_string");
    $centi = $inches *  2.53998;
    <H1>Inch Converter</h1>
       <?php echo $inches; ?> inches = <?php echo $centi; ?> centimeters

Steps for this Lab

  1. Enter this URL and query string into your browser:
    1. Describe what is printed.
    2. Note that the 10 appears as part of the URL. What happens if you change the 10 to 100 or -10 in the URL?

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.

More Steps for this Lab

  1. 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.

  2. Edit the program to comment the global registration out:

        // ini_set("register_globals", "on");

    Describe what happens.

  3. 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!

Forms: Query Strings and the GET Method

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:

Feet: Inches:

Steps in the Lab Continue

  1. Enter values for feet and inches in this form, and click "submit"

    1. Describe what data are displayed on the page.
    2. Describe the query string produced by this form, after clicking "submit"
    3. What happens if you click "submit" without entering data, or if you fill in one field, but not the other?

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>
    <input type="submit" value="Submit">

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:

    $inches = $_GET["inches"];
    $feet   = $_GET["feet"];
    $centi = ($feet * 12 + $inches) * 2.53998;

    <H1>Foot-Inch Converter</h1>

       <?php echo $feet; ?> feet, <?php echo $inches; ?> inches = <?php echo $centi; ?> centimeters

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:

The POST Method

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:

Feet: Inches:

Similarly, the program retrieves data from the form with these lines:

    $inches = $_POST["inches"];
    $feet   = $_POST["feet"];

Still More Steps for this Lab

  1. Use the form to enter values for feet and inches, and click submit.

    1. Check that the HTML/PHP script produces appropriate results.
    2. Review the URL to observe no data are visible.
  2. 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.

Conditionals: if Statements

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;
        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;



  1. 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:

Work to Turn In

  1. Commentary for steps 3, 4, 5
  2. URLs, HTML forms and HTML/PHP scripts for steps 7 and 8.

This document is available on the World Wide Web as

created 10 August 2008
last revised 22 September 2008
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at