PHP自学1——简单表单提交

  最近自学PHP,顺便做个笔记记录一下自己的学习进度。选用的教程是《PHP and MySQL Web Development 4th Edition》,建议阅读英文教材(我能说英文网上免费的中文资源不清晰么,为避免版权纠纷,链接请自寻)。

  本节代码实现了从一个html页面将用户填写信息提交到服务器,服务器根据提交信息显示一些统计数据(通过php脚本)。

orderForm.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">What's your gender?</td>
    <td align="center">
    <select name="gender">
    <option value="male">Male</option>
    <option value="female">Female</option>
    </select>
    </td>
</tr>
<tr>
    <td align="center" colspan="2"><input type="submit" value="Submit Order" /></td>
</tr>
</table>
</form>
</body>
</html>

processOrder.php文件(该文件中还包含html标签):

<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'];
    $sex = $_POST['gender'];
    #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;
    #define constant value
    define("MILKPRICE" , 3.50);
    define("COKEPRICE" , 2.50);
    define("TEAPRICE" , 4.00);
    define("COFFEEPRICE" , 10.00);
    define("JUICEPRICE" , 8.00);
?>
<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;
    //Print the result
    echo "<p>Order processed at ". date("G:i Y dS");
    echo "<p>Your Order is as follows</p>";
    echo $milkQty. " Milk<br />";
    echo $cokeQty. " Coke<br />";
    echo $teaQty. " Tea<br />";
    echo $coffeeQty. " Coffee<br />";
    echo $juiceQty. " Juice<br />";
    echo "Item order: ". $totalQty. "<br />";
    echo "Total price: $". $totalPrice. "<br />";
    echo "It's a ". $sex. " customer."
?>
</body>
</html>

html页面显示及提交响应结果:

2016/2/29修订  By野马菌

 

你可能感兴趣的:(PHP自学1——简单表单提交)