Вы находитесь на странице: 1из 6

Name:____Tan Wan Sean ________________________________

Date: ________________

Tutorial P2 – Creating an Order Form (1.5 hours)

Aim: To create an order form to store and retrieve data from the server

 Ensure that XAMPP is installed in your PC. Open XAMPP Control Panel and ensure that
Apache, MySql and Filezilla are running (in green).

1. First write your first php page using Notepad. Check to see the page is working fine.
Save it as order.html inside folder C:XAMPP/htdocs/ or upload to your online
server’s /htdocs folder.

<html>
<body>
<form action="processorder.php" method="post">
<table border="0">
<tr bgcolor="#cccccc">
<td width="150">Item</td>
<td width="15">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="center"><input type="text" name="tireqty" size="3"
maxlength="3"></td>
</tr>
<tr>
<td>Oil</td>
<td align="center"><input type="text" name="oilqty" size="3" maxlength="3"></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td align="center"><input type="text" name="sparkqty" size="3"
maxlength="3"></td>
</tr>
<tr>
<td>How did you find Bob's?</td>
<td><select name="find">
<option value = "a">I'm 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>
<tr>
Name:____Tan Wan Sean ________________________________
Date: ________________

<td colspan="2" align="center"><input type="submit" value="Submit Order"></td>


</tr>
</table>
</form>
</body>
</html>

2. Make sure XAMPP is running. Open your browser and type http://localhost/ in the address bar.
If XAMPP is running you will see a Welcome page. Now type http://localhost/order.html - to
see that the page can be retrieved.

3. Notice that the form’s action is set to the name of the php script that will process the
customer’s orders. Note that the form also created 3 variables to be entered into the forms and
to be sent to the server - tireqty , oilqty and sparkqty. Next, we will write the script to process
the order – save this file as processorder.php

<?php
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$find = $_POST['find'];
?>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
Name:____Tan Wan Sean ________________________________
Date: ________________

<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php

echo "<p>Order processed at ".date('H:i, jS F Y')."</p>";

echo "<p>Your order is as follows: </p>";

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo "Items ordered: ".$totalqty."<br />";

if ($totalqty == 0) {

echo "You did not order anything on the previous page!<br />";

} else {

if ($tireqty > 0) {
echo $tireqty." tires<br />";
}

if ($oilqty > 0) {
echo $oilqty." bottles of oil<br />";
}

if ($sparkqty > 0) {
echo $sparkqty." spark plugs<br />";
}
}

$totalamount = 0.00;

define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);

$totalamount = $tireqty * TIREPRICE


+ $oilqty * OILPRICE
+ $sparkqty * SPARKPRICE;

echo "Subtotal: $".number_format($totalamount,2)."<br />";

$taxrate = 0.10; // local sales tax is 10%


$totalamount = $totalamount * (1 + $taxrate);
Name:____Tan Wan Sean ________________________________
Date: ________________

echo "Total including tax: $".number_format($totalamount,2)."<br />";

if($find == "a") {
echo "<p>Regular customer.</p>";
} elseif($find == "b") {
echo "<p>Customer referred by TV advert.</p>";
} elseif($find == "c") {
echo "<p>Customer referred by phone directory.</p>";
} elseif($find == "d") {
echo "<p>Customer referred by word of mouth.</p>";
} else {
echo "<p>We do not know how this customer found us.</p>";
}

?>
</body>
</html>

4. From the form processing script we can see that the following script is collecting data from the
form and assigning them to named variables:

<?php
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$find = $_POST['find'];
?>

5. We then calculate the total quantity ordered::

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;

6. Following section sets the price for the items purchased and then total the amount:
Name:____Tan Wan Sean ________________________________
Date: ________________

define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);

$totalamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE;

7. Following section calculates the tax amount and returns the total amount:

$taxrate = 0.10; // local sales tax is 10%


$totalamount = $totalamount * (1 + $taxrate);
echo "Total including tax: $".number_format($totalamount,2)."<br />";

8. Explain the last section of the code:

The formula is $totalamount = $totalamount * (1 + $taxrate). The tax rate is 10%, the code
calculates the total amount plus tax.

9. Upload both the files to your server, validate the outcomes and comment:

The calculation works. After you fill in the quantity of the items and how you find Bob’s , and
submit order, it will pops out order results, helps you to calculate the subtotal and total
including tax

Screenshot your output:


Name:____Tan Wan Sean ________________________________
Date: ________________

References:
1. http://www.w3schools.com/php/
2. http://html.net/tutorials/php/lesson1.php

Вам также может понравиться