PHP自学2——将用户提交表单存储到外部普通文件中

  在上一节中我们已经实现了将用户的订单信息提交到服务器端,然后服务器端将提交信息返回并显示到页面上。这一节将把上一节用户的订单信息保存到外部的普通文件中(即.txt文本文件中)。

  本节代码将用户提交的订单消息存储到外部文件中,并且通过另外php脚本显示所有订单信息。

表单界面——rderForm.html(和上一节中有微小的区别):

<html>
<head>
<title>Wayne's Online DrinkShop</title>
</head>
<body>
<!--Cereate a Submit Form-->
<form action="processOrder.php" method="POST">
<table border="0" cellpadding= "3" >
<tr>
    <th align="center" colspan="2" bgcolor="#CCCCCC">Drink List</th>
</tr>
<tr>
    <td align="center" bgcolor="#CCCCCC" width="200" align="center">Drink Name</td>
    <td align="center" bgcolor="#CCCCCC" width="100" align="center">Quantity</td>
</tr>
<tr>
    <td align="center">Milk</td>
    <td align="center"><input type="text" name="milkQty" size="3"/></td>
</tr>
<tr>
    <td align="center">Coke</td>
    <td align="center"><input type="text" name="cokeQty"  size="3"/></td>
</tr>
<tr>
    <td align="center">Tea</td>
    <td align="center"><input type="text" name="teaQty"  size="3"/></td>
</tr>
<tr>
    <td align="center">Coffee</td>
    <td align="center"><input type="text" name="coffeeQty"  size="3"/></td>
</tr>
<tr>
    <td align="center">Juice</td>
    <td align="center"><input type="text" name="juiceQty"  size="3"/></td>
</tr>
<tr>
    <td align="center">Shipping Address?</td>
    <td><input type="text" name="address" size="50"></td>
    
</tr>
<tr>
    <td align="center" colspan="2"><input type="submit" value="Submit Order" /></td>
</tr>
</table>
</form>
</body>
</html>

表单处理脚本——processOrder.php:

<html>
<head>
<title>Wayne's DrinkShop-Order Result</title>
</head>
<body>
<?php
    //Get the value from POST method from html page
    $milkQty = $_POST['milkQty'];
    $cokeQty = $_POST['cokeQty'];
    $teaQty = $_POST['teaQty'];
    $coffeeQty = $_POST['coffeeQty'];
    $juiceQty = $_POST['juiceQty'];
    $address = $_POST['address'];
    #set the variable's value to 0 when it is null
    $milkQty = $milkQty ? $milkQty : 0;
    $cokeQty = $cokeQty ? $cokeQty : 0;
    $teaQty = $teaQty ? $teaQty : 0;
    $coffeeQty = $coffeeQty ? $coffeeQty : 0;
    $juiceQty = $juiceQty ? $juiceQty : 0;
    $date = date("G:i jS Y");
    #define constant value
    define("MILKPRICE" , 3.50);
    define("COKEPRICE" , 2.50);
    define("TEAPRICE" , 4.00);
    define("COFFEEPRICE" , 10.00);
    define("JUICEPRICE" , 8.00);
    $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<h1>Wayne's DrinkShop</h1>
<h2>Order Result</h2>

<?php
    //Calculate the value
    $totalQty = $milkQty + $cokeQty + $teaQty + 
                $coffeeQty + $juiceQty;
    $totalPrice = $milkQty * MILKPRICE + $cokeQty * COKEPRICE 
                + $teaQty * TEAPRICE + $coffeeQty * COFFEEPRICE
                + $juiceQty * JUICEPRICE;
    $totalPrice = number_format($totalPrice, 2, '.', ',');
    //Print the result
    echo "<p>Order processed at ". $date;
    if($totalQty == 0){
        echo "<p>You did not buy anything<p>";
    }
    else{
        echo "<p>Your Order is as follows</p>";
        echo "Item order: ". $totalQty. "<br />";
        if($milkQty){
            echo $milkQty. " Milk<br />";
        }
        if($cokeQty){
            echo $cokeQty. " Coke<br />";
        }
        if($teaQty){
            echo $teaQty. " Tea<br />";
        }
        if($coffeeQty){
            echo $coffeeQty. " Coffee<br />";
        }
        if($juiceQty){
            echo $juiceQty. " Juice<br />";
        }
        echo "Total price: $". $totalPrice. "<br />";
        if($address){
            echo "Address to ship to is ". $address. "<br />";
        }
    }
    //prepare order for outputing
    $outputString = $date. "\t". $milkQty. " Milk\t". $cokeQty. " Coke\t".
                    $teaQty. " Tea\t". $coffeeQty. " Coffee\t". $juiceQty. "Juice\t \$".
                    $totalPrice. "\t ". $address. "\n";
    //open file for output order records to file
    @ $fp = fopen("$DOCUMENT_ROOT/../orders/DrinkOrder.txt", "ab");
    if(!$fp){
        echo "<p><strong>You can't open this file for writing.</strong></p>";
        exit;
    }
    flock($fp, LOCK_EX);    //create a writing lock
    fwrite($fp, $outputString, strlen($outputString));
    flock($fp, LOCK_UN);    //release the writing lock
    echo "Order has been written in file.<br />";
    fclose($fp);
?>
</body>
</html>

注:(1)上面程序中的$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];语句获取了你的网络服务器文档树的根目录,$DOCUMENT_ROOT避免了完整路径的书写

  (2)"$DOCUMENT_ROOT/../orders/DrinkOrder.txt"中的..表示文档根目录(即$DOCUMENT_ROOT)的父目录,

     以笔者电脑为例,笔者电脑的$DOCUMENT_ROOT为  D:\Workspace\PHP

     那么$DOCUMENT_ROOT/../orders/DrinkOrder.txt就表示D:\Workspace\orders\DrinkOrder.txt,笔者电脑操作系统(Win7)需要在文档根目录的父目录下建     立一个orders目录,否则将找不到路径

订单数据读取脚本——viewOrders.php:

<html>
<head>
    <title>Wayne's Drink Shop-Customer Orders</title>
</head>
<body>
<h1>Wayne's Drink Shop</h1>
<h2>Customer Order</h2>
<?php
    $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
    //Open file for reading
    @ $fp = fopen("$DOCUMENT_ROOT/../orders/DrinkOrder.txt", "rb");
    if(!$fp){
        echo "<p><strong>Can't open this file for reading.</strong></p>";
        exit;
    }
    flock($fp, LOCK_SH);    //creat a reading lock
    while(!feof($fp)){
        $order = fgets($fp, 999);
        echo $order. "<br />";
    }
    flock($fp, LOCK_UN);    //release a reading lock
    fclose($fp);
?>
</body>
</html>

表单显示结果:

订单存储结果:

查看所有订单结果:

修订于2016/3/1  By 野马菌

你可能感兴趣的:(PHP自学2——将用户提交表单存储到外部普通文件中)