代码结构
php_project
|–orderform.html
|–processorder.php
|–freight.html
//processorder.php
<?php
//提取表格数据
$tireqty =$_POST['tireqty']?$_POST['tireqty']:0;
$oilqty =$_POST['oilqty']?$_POST['oilqty']:0;
$sparkqty=$_POST['sparkqty']?$_POST['sparkqty']:0;
$find=$_POST['find'];
//定义单价
define('TIREPRICE',100);
define('OILPRICE',10);
define('SPARKPRICE',4);
//tireqty折扣
if($tireqty<10)$discount=0;
elseif ($tireqty>=10&&$tireqty<=49)$discount=5;
elseif ($tireqty>=50&&$tireqty<=99)$discount=10;
elseif ($tireqty>=100)$discount=15;
//总数量
$totalqty=0;
$totalqty=$tireqty+$oilqty+$sparkqty;
//总价
$totalamount=0.00;
$totalamount=$tireqty*TIREPRICE*(1-$discount*0.01)
+$oilqty*OILPRICE
+$sparkqty*SPARKPRICE;
//税后总价
$taxrate=0.10;
$totalamount*=(1+$taxrate);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php echo "<p>Order processed at ".date('H:i, jS F Y')."</p>"; ?>
<?php
if ($totalqty == 0){
echo '<p style="color:#ff0000">';
echo 'You did not order anything on the previous page!<br />';
echo '</p>';
}else{
echo "<p>Your order is as follows: </p>";
if($tireqty>0)echo htmlspecialchars($tireqty).' tires<br />';
if($oilqty>0)echo htmlspecialchars($oilqty).' bottles of oil<br />';
if($sparkqty>0)echo htmlspecialchars($sparkqty).' spark plugs<br />';
echo "<p>Item ordered:"."$totalqty"."<br />";
echo "Subtotal:$".number_format($totalamount,2)."<br />";
echo "Total including tax:$".number_format($totalamount,2)."</p>";
}
//对多选处理
switch ($find){
case 'a':
echo "<p>Regular customer.</p>";break;
case 'b':
echo "<p>Customer referred by TV advert.</p>";break;
case 'c':
echo "<p>Customer referred by phone directory.</p>";break;
case 'd':
echo "<p>Customer referred by word of mouth.</p>";break;
default:
echo "<p style='color: #ff0000'>We do not know this customer found us.</p>";break;
}
?>
</body>
</html>
//orderform.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FORM</title>
</head>
<body>
<form action="processorder.php" method="post">
<table style="border: 0px;">
<tr style="background: #cccccc;">
<td style="width: 150px;text-align: center;">Item</td>
<td style="width: 15px;text-align: center;">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td><input type="text" name="tireqty" size="3" maxlength="3" /></td>
</tr>
<tr>
<td>Oil</td>
<td><input type="text" name="oilqty" size="3" maxlength="3" /></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td><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 hidden></option>
<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>
<td colspan="2" style="text-align: center"><input type="submit" value="SubmitOrder" /></td>
</tr>
</table>
</form>
<a href="freight.html">Freight</a>
</body>
</html>
//freight.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bob's Auto Parts - Freight Costs</title>
</head>
<body>
<table style="border: 0; padding: 3px;">
<tr>
<td style="background: #cccccc; text-align: center;">Distance</td>
<td style="background: #cccccc; text-align: center;">Cost</td>
</tr>
<?php
for($distance=50;$distance<=250;$distance+=50) {
echo "<tr>
<td style='text-align: right;'>".$distance."</td>
<td style='text-align: right;'>".($distance/10)."</td>
</tr>\n";
}
?>
</table>
</body>
</html>