Practical 08: Introducing PHP into Forms
In this practical activity, we will be implementing an order form for Wendy's Bakery, a fictional bakery company. Right now, Wendy's HTML programmer has gotten as far as setting up an order for for the cakes Wendy sells. The order form is shown as in the figure below. This is a relatively simple order form, similar to many which you hve probably seen while surfing. The first thing Wendy would like to be able to do is to know what her customer ordered, work out the total of the customer's order, and how much sales tax is payable on the order. Part of the HTML for this is shown in Listing 1. In the form, we have set the form's action to be the name of the PHP script that will process the customer's order and also the name of the form fields.
Listing 1
We'll proceed to create our PHP file surrounding this form element.
Task 1: Embedding PHP in HTML
To process the form, we'll need to create the script mentioned in the action
attribute of the <form>
tag called process-order.php
.
Prepare process-order.php
starting off with the following HTML code:
Task 2: Conditional Display of Information
Modify process-order.php
such that it displays the following when the order form is filled and the Submit button is clicked on.
Task 3: Adding Dynamic Content
Modify process-order.php
to display the date and time when the form was submitted.
DateTime in PHP
Read more here: PHP DateTime
Task 4: Accessing Variables
Within the same PHP script, add some lines to display the customer's order list.
Task 5: Assigning Values to Variables
On Wendy's site, we want to work out the total number of items ordered and the total amount payable.
Assume the prices of each item are as follows:
Cupcake = $2.50 Puff = $3.00 Muffin = $4.00 Sales Tax = 6% of the total amount
Within the same script, add some lines to display the following additional output:
Task 6: Making Decisions with Conditionals
Modify the script such that it alerts if the customer has not ordered anything from Wendy.
Otherwise, the script should display the items the customer ordered from Wendy.
Note
You may have noticed that there are some errors when you only fill up the form partially (to make it such that you only want some of the items, not all). Try to find a way to circumvent this issue (i.e., by placing an if statement, modifying the form input element, etc.).
See if you can find any loopholes in the web application at this point and look to see if you can fix them as much as possible.
Task 7: elseif
Statements
Wendy provides discounts for large orders of cupcakes.
- Less than 10 cupcakes: no discount
- 10-49 cupcakes: 5% discount
- 50-99 cupcakes: 10% discount
- ≥100 cupcakes: 20% discount
Modify process-order.php
such that it calculates the discount using conditions and if-else and elseif statements.
Task 8: switch
Statements
Wendy wants to know what forms of advertising are working for her.
Insert a <select>
element to the form to retrieve that information.
<tr>
<td colspan="2">
<p><label for="find">How did you find Wendy's?</label></p>
<select name="find" id="find">
<option disabled selected>Select option</option>
<option value="a">I am a regular customer.</option>
<option value="b">TV Advertising</option>
<option value="c">Phone Directory</option>
<option value="d">Word of Mouth</option>
</select>
</td>
</tr>
Use the appropriate selection statements to handle the variables and display the information in process-order.php
.
Task 9: Iteration and Repeating Actions
Wendy wants a table displaying the freight cost that is to be added to the customer's order. With the courier Wendy's uses, the cost of freight depends on the distance the parcel is being shipped. We want our freight table to resemble the following:
Generate the same freight table using loop structures.
<table id="freight-table">
<thead>
<tr>
<th>Distance</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<?php
$freight = [
["distance" => 50, "cost" => 5],
["distance" => 100, "cost" => 10],
["distance" => 150, "cost" => 15],
["distance" => 200, "cost" => 20],
["distance" => 250, "cost" => 25],
];
// var_dump($freight);
foreach ($freight as $f) {
?>
<tr>
<td><?= $f["distance"]; ?></td>
<td><?= $f["cost"]; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>