Aim: To Create An Order Form To Store and Retrieve Data From The Server
Aim: To Create An Order Form To Store and Retrieve Data From The Server
Date: ________________
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: ________________
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
$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);
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'];
?>
$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);
7. Following section calculates the tax amount and returns the total amount:
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
References:
1. http://www.w3schools.com/php/
2. http://html.net/tutorials/php/lesson1.php