| CS 199 | Willamette University | Spring, 2019 |
|
Programming in PHP, Databases with MySQL, and Web Applications |
||
This lab builds upon the earlier Lab on Simple Forms and User Data, expanding the type of user data that can be processed.
Review topics:
New topics:
All of these concepts allow users to input data of varying types. Be sure to read pages 52-66 in the textbook in preparation for this lab.
As a review, consider the following form
University: Current feeling: Happy Sad Neutral |
Write an HTML form and PHP response page, based on this design and using the approach discussed in the earlier Lab on Simple Forms and User Data.
This form and response uses the GET method to transmit data.
For this section we will be creating a profile for a user.
Create a form with the following elements:
Create a response page that receives the information the user has typed, and print out "Hello $firstName $LastName".
Checkboxes allow a user to select a collection of options.
Many sources on the Web provide background on the use of checkboxes; perform a Web search for details on checkboxes beyond the following example that allows a user to identify some favorite rooms.
|
Which of the following are your favorite rooms (select none, some, or all): Living RoomDining Room Kitchen Bed Room Bathroom |
Within HTML itself, these checkboxes are defined, where the name variable is identified as an array:
<input type="checkbox" name="favoriteRoom[]" value="LR">Living Room<br> <input type="checkbox" name="favoriteRoom[]" value="DR">Dining Room<br> <input type="checkbox" name="favoriteRoom[]" value="Kit">Kitchen<br> <input type="checkbox" name="favoriteRoom[]" value="BR">Bed Room<br> <input type="checkbox" name="favoriteRoom[]" value="Bath">Bathroom<br>
The complete code for this form is available as a Web page or in text form.
Turning to the response page, processing proceeds by treating the checkbox variable as an array, although an initial detail extends beyond common array processing. Overall processing of checkboxes is illuatrated in the following PHP code segment.
// first check if the user selected any of the checkboxes
if (isset($_POST['favoriteRoom']))
{
$choices = $_POST['favoriteRoom'];
echo "processing the values directly from the array<br>";
$arrayLength = sizeof ($choices);
for ($i = 0; $i < $arrayLength; $i++)
{
echo "$i:$choices[$i]<br>";
}
echo "accessing the values in a <tt>foreach</tt> statement<br>";
foreach ($choices as $key => $room)
{
echo "$key: $room<br>";
}
}
else
{
echo "no checkboxes checked<br>";
}
This program may be considered in several segments.
As with any response processing, data are obtained from the _$POST (or $_GET) variable.
In the case of checkboxes, it may be that the user has not checked anything, so the input is empty. Thus, although not always necessary, processing often begins by determining if any boxes were checked. This is accomplished with the isset function.
Once the array is known to be non-empty, processing may proceed following usual array techniques.
The complete PHP response program may be viewed in text form.
Expanding on steps 3 and 4 of this lab, create a check box that allows you to identify the colors of the rainbow that you really like. The response page should print your list of favorites.
Using checkboxes, allow a user to select multiple cuisines they enjoy: Mexican Cuisine, Chinese Cuisine, Japanese Cuisine, Indian Cuisine, Italian Cuisine, Mediterranean Cuisine, etc. Again, the response page should print out all the cuisines the user selected, "You like _____". (Feel free to use different cuisines).
Suppose friends come to Yawbus (read it backwards ;)), a sandwich shop. They are given the following menu:
Sandwiches Soft Drinks Sizes
---------- ------------- -----
Veggie Delight $6.50 Coke Small $1
Ham and Cheese $6.00 Dr. Pepper Medium $1.50
BLT $7.50 Sprite Large $1.75
Meatball Sub $7.00 Pepsi
Grilled Cheese $4.00 Orange Crush
In addition to input fields already covered for forms, a textarea box allows a user to enter an extended amount of text. For example, the code
<textarea name="example" cols = 50 rows = 12></textarea>
specifies a box that is 50 columns wide and 12 rows tall:
After clicking submit, we can retrieve data entered into this box with the $_GET and $_POST variables, just as for other types of input data.
Once retrieved, this text may be processed as a strng of characters. Some common string functions are described inthe PHP Manual. (Look under "Text Processing" in the "Function Reference Chapter". With this section, consult the "String Functions" link under "Strings".)
Create a form with a textarea containing at least 200 characters, and create a response page that prints the user's entire text.
Consider the problem of retrieving text from a textarea box, and counting the number of times each word appears.
One possible algorithm for solving this problem is:
|
created 13 February 2019 by Saniya Lakka revised 14-18 February 2019 by Saniya Lakka and Henry M. Walker |
|