PHP表单实例——存储和获取ZM的订单
功能需求
按照功能以及使用的目标人群大致分为三部分:
- 针对用户需要设计一个html网页,用来输入表单。
- 针对后端服务器,设计一个数据处理的php文件,目的是将用户输入的数据发送到txt文本文件中。
- 针对处理订单的工作人员,需要一个简洁、美观的表格来查看并处理订单。
代码实现及部分讲解
一、orderform.html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div style="float:left;margin-left: 380px;margin-top: 200px;">
<form action="processorder.php" method="post">
<table>
<tr style="background-color: rgba(238, 136, 238, 0.534);">
<td style="width: 300px;text-align: center;">Item</td>
<td style="width: 380px;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>Shipping Address</td>
<td><input type="text" name="address" size="60" maxlength="45"></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;"><input type="submit" value="Submit Order"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
二、processorder.php :
<?php
$tireqty = (int) $_POST['tireqty'];
$oilqty = (int) $_POST['oilqty'];
$sparkqty = (int) $_POST['sparkqty'];
$address = $_POST['address'];
$document_root = $_SERVER['DOCUMENT_ROOT'];
$date = date('H:i,jS F Y');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Order Result</title>
</head>
<body>
<h1>Zm's Auto Parts</h1>
<h2>Order Result</h2>
<?php
echo "<p>Order processed at ".date('H:i,jS F Y')."</p>";
echo "<p> Your order is as follows:</p>";
$totalqty = 0;
$totalamount = 0.00;
define('TIREPRICE',100);
define('OILPRICE',10);
define('SPARKPRICE',4);
$totalqty = $tireqty + $oilqty + $sparkqty;
echo "<p>Items ordered:".$totalqty."<br/>";
if($totalqty == 0){
echo "You did not order anything on the previous page!<br/>";
}
else{
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/>';
}
}
$totalamount = $tireqty * TIREPRICE
+ $oilqty * OILPRICE
+ $sparskqty * SPARKPRICE;
echo "Subtotal:$".number_format($totalamount,2)."<br/>";
echo "<p>Address to ship to is ".htmlspecialchars($address)."</p>";
$outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t".$sparkqty." spark plugs\t\$".$totalamount."\t". $address."\n";
$fp = fopen("order.txt",'ab');
if(!$fp){
echo "<p><strong>Your order could not be processed at this time. </strong></p>";
}
flock($fp, LOCK_EX);
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
echo "<p>Order written.</p>";
?>
</body>
</html>
Tips:
- $fp = fopen( " order.txt " , ’ ab ’ ); 打开文件
从上表中可以看出,如果我们选用 “w“ ,这表示只可以将一个订单写入文件中,每当一个新订单被写入文件,它将覆盖以前的订单,这样就没有任何意义了。所以在这里我们使用追加模式(以及推荐的二进制模式),即:$fp = fopen( " order.txt " , ’ ab ’ ) 。
- flock($fp, LOCK_EX); 文件锁定
- fwrite($fp, o u t p u t s t r i n g , s t r l e n ( outputstring, strlen( outputstring,strlen(outputstring)); 写文件
有三个参数,第一个就是要写入的目标文件,第二个是要写入的内容,第三个是写入的最大字符数。
strlen()函数获得字符串的长度
- $tireqty = (int) $_POST[‘tireqty’]; 收集表单数据
PHP $_POST 广泛用于收集提交 method=“post” 的 HTML 表单后的表单数据。
超级全局变量:
- $GLOBALS ——引用全局作用域中可用的全部变量
- _SERVER ——这种超全局变量保存关于报头、路径和脚本位置的信息
- _REQUEST —— 用于收集 HTML 表单提交的数据,POST方法和GET方法都可以
- _POST —— 广泛用于收集提交 method=“post” 的 HTML 表单后的表单数据。
- _GET —— 也可以用于收集提交 method=“get” 的 HTML 表单后的表单数据。
- _FILES
- _COOKIE
- _SESSION
三、vieworders.php :
<?php
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bob's Auto Parts - Customer Orders</title>
<style>
table , th , td{
border-collapse:collapse;
border: 1px solid black;
padding: 6px;
}
th{
background: #ccccff;
}
</style>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Customer Orders</h2>
<?php
$orders = file("/Applications/MAMP/htdocs/Bob/order.txt");
$number_of_orders = count($orders);
if($number_of_orders == 0){
echo "<p><strong>No Orders Pending.</br>
Please try again later.</strong></p>";
}
echo "<table>\n";
echo "<tr>
<th>Order Date</th>
<th>Tires</th>
<th>Oil</th>
<th>Spark Plugs</th>
<th>Total</th>
<th>Address</th>
</tr>";
for($i=0; $i<$number_of_orders;$i++){
$line = explode("\t",$orders[$i]);
$line[1] = intval($line[1]);
$line[2] = intval($line[2]);
$line[3] = intval($line[3]);
echo "<tr>
<td style=\"text-align:center;\">".$line[0]."</td>
<td style=\"text-align:center;\">".$line[1]."</td>
<td style=\"text-align:center;\">".$line[2]."</td>
<td style=\"text-align:center;\">".$line[3]."</td>
<td style=\"text-align:center;\">".$line[4]."</td>
<td style=\"text-align:center;\">".$line[5]."</td>
</tr>";
}
echo "</table>";
?>
</body>
</html>
Tips:
$line = explode( “\t” , o r d e r s [ orders[ orders[i] );
explode()函数:使用一个字符串分割另一个字符串,并返回由字符串组成的数组。
语法:explode( separator , string , limit )
功能界面
orderform.html
order.txt:
vieworder.php: