| CS 199 | Willamette University | Spring, 2019 |
|
Programming in PHP, Databases with MySQL, and Web Applications |
||
Summary: This lab is the second of two activities that introduce the possibility that a Web page can respond to changing circumstances.
The previous lab considered tailoring Web-page content, based on such local conditions and the time and/or the day.
This lab explores the possibility of responding to user-supplied data within a Web page.
Read Butler and Yank, pages 62-65, before starting this laboratory exercise!
Processing user data in the context of Web pages typically involves two steps, each with a separate Web page:
A Form is included in one Web page.
A response page containing a program, perhaps containing PHP, that retrieves the data entered in the Web form and processes that data.
This lab proceeds in 3 stages:
Download the Web page form form-example-1.php, and also the TextEdit version of the response file, form-example-1-response.php.
Load the page source of form-example-1.php into TextEdit, and compare the elements to the form described in Rutler and Yank, pages 62-65.
For "text" input, a value may be designated as the default; if the user does not enter a value in the textbox, the default is used.
In the original Web form, no radio buttons are set at the start.
The Web form places the radio buttons on nine separate lines.
When a form submits information with the GET method, the user-supplied data are appended to the URL. This extended URL has the format
http://www.walker.cs.grinnell.edu/courses/199.sp19-willamette/labs/form-example-1-response.php?yourText=...&yourNumber=...
The first part of this line has the familiar form of a URL, and a question mark separates this location from the user data. Information from the form is organized into pairs:
key=value
Here, the key is the name given in the form. The user-supplied information follows an equal sign. In this case, the form has two text boxes and a radio button section, so the extended URL has two key-value pairs for the boxes and one for the radio button (if some button is clicked). An ampersand (&) separates data from different boxes.
Enter data in the Web form, and click "submit", and examine the full URL in the browser.
The above description for a URL indicates that a question mark, equal sign, and ampersand may have special significance in a URL.
Browsers normally display URL in a page header.
For this part of the lab, work with the text version of the page, form-example-1-response.php, loaded into TextEdit.
The first php section of the Web page contains the lines:
$number1 = $_GET["box1"]; $number2 = $_GET["box2"]; $shape = $_GET["shape"];
Since the form indicated data transmission via a "get" method, this section of the response page retrieves data from a $_GET[ ] variable, where the brackets contain the name of a box in the Web form.
What happens if a variable name is misspelled or apparently confused, such as
$number2 = $_GET["box1"]; $number1 = $_GET["box2"]; $shape = $_GET["other"];
Once variables are set, explain how subsequent processing proceeds in this response page.
The response page prints a sum, assuming that the user entered numbers in the two text boxes.
This response page produces two tables of data.
Extend processing in forms-example-1.php, so the difference, product and quotient of the two numbers are computed and printed, as well as the sum.
Create two new tables, near the end of forms-example-1-response.php, giving each number and its square, its value cubed, and its value to the fourth poser. The table might have the following general format:
| name | number | squared | cubed | fourth power |
|---|---|---|---|---|
| First Value | ||||
| Second Value |
|
created 23 January 2019 revised 23-24 January 2019 |
|