简单的PHP订单小程序

HTML代码

<html>

<head>
<meta charset="UTF-8">
</head>
          <body>
<form action="./main.php" method="post">
    <table style="border:0px;">
    <tr style="background: #cccccc;">
        <td style="width:150px;text-align: center;">项目</td> 
        <td style="width: 15px; text-align: center;">数量</td>
    </tr>


    <tr>
        <td>轮胎</td>
        <td> <input type="text" name="tireqty" size="3" maxlength="3" /> </td>
        
    </tr>


    <tr> 
        <td>汽油</td>
        <td> <input type="text" name="oilqty" size="3" maxlength="3"/> </td>
    </tr>


    <tr>
        <td>火花塞</td>
        <td> <input type="text" name="sparkqty" size="3" maxlength="3"/> </td>
    </tr>


    <tr> 
        <td>收货地址</td>
        <td> <input type="text" name="address" size="15" maxlength="15"> </td>


    </tr>


    <tr>
        <td colspan="2" style="text-align: center;"> <input type="submit" value="Submit Order"/> </td>
    </tr>
    </table>
</form>

            </body>
</html>

PHP代码

<?php

$tireqty = (int) $_POST['tireqty'];
$oilqty = (int) $_POST['oilqty'];
$sparkqty = (int) $_POST['sparkqty'];
$address = preg_replace('/\t|\R/','',$_POST['address']);
$document_root = $_SERVER['DOCUMENT_ROOT'];

?>

<!DOCTYPE html>
<html>
    
    <head>
        <title>这是Bob的订单详情</title>
        
    </head>


    <body>
        <h1>Bob的订单</h1>
        <h2>订单结果</h2>
        
        <?php
            echo "

订单处理时间不知道

"
; echo "

订单处理结果如下:

"
; $totalqty = 0; #总数 $totalamount = 0.0; #总金额 define('TIREPRICE', 100); #轮胎单价 define('OILPRICE', 50); #油耗单价 define('SPARKPRICE', 20); #火花单价 $document_root = $_SERVER['DOCUMENT_ROOT']; $totalqty = $tireqty + $oilqty + $sparkqty; echo "

总数量:$totalqty

"
; if ($totalqty == 0) { echo "没有任何订单!
"
; }else { if ($tireqty > 0) { echo "轮胎数量:$tireqty
"
; } if ($oilqty > 0) { echo "油耗数量:$oilqty
"
; } if ($sparkqty > 0) { echo "火花数量:$sparkqty
"
; } } $totalamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE; echo "

总金额:".number_format($totalamount, 2)."

"
; $taxrate = 0.10; #税率 $totalamount = $totalamount * (1 + $taxrate); echo "

含税金额:".number_format($totalamount, 2)."

"
; $outputstring = $tireqty . " 轮胎\t" . $oilqty . " 油耗\t" . $sparkqty ."火花\t"."共计 " . $totalqty . " 件\t总金额 " . number_format($totalamount, 2) . " 元
"
; //打开文件,写入内容 @$fp = fopen( $document_root."/data.txt", "ab"); if (!$fp) { echo "打开文件写入失败!
"
; exit; } flock($fp, LOCK_EX); fwrite($fp, $outputstring,strlen($outputstring)); flock($fp, LOCK_UN); fclose($fp); echo "订单已保存!
"
; ?> </body> </html> <!DOCTYPE html> <html> <head> <title>这是Bob的订单详情</title> </head> <body> <h1>Bob的订单</h1> <h2>订单结果</h2> <?php $document_root = $_SERVER['DOCUMENT_ROOT']; $fp = fopen($document_root . "/data.txt", "rb"); if (@!$fp) { echo "打开文件读取失败!
"
; exit; } while (!feof($fp)) { $order = fgets($fp); echo $order; } flock($fp, LOCK_UN); fclose($fp); ?> </body> </html>

代码风格不是很好大家见谅

你可能感兴趣的:(php,小程序,chrome)