代码改变世界

第一章 PHP快速入门 —创建一个示例应用:Bob 的汽车零部件商店

2016-07-17 22:22  yojiaku  阅读(710)  评论(0编辑  收藏  举报

学习重点:

1.在HTML中嵌入PHP:a.PHP标记 b.注释

2.添加动态内容:a.使用函数

3.访问表单变量:a.字符串的连接

Bob的HTML程序员已经设置好Bob汽车零部件商店所销售的零部件订单。

程序清单1-1 myform.html—Bob 基本订单表单的HTML代码

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>BOb's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<form action = "processorder.php" method = "post">
    <table border="0">
        <tr bgcolor="#cccccc">
            <td width="150">Item</td>
            <td width="80" align="center">Quantity</td>
        </tr>
        <tr>
            <td>Tires</td>
            <td align="center"><input type="text" name="tireqty" size="3" maxlength="3" /></td>
        </tr>
        <tr>
            <td>Oil</td>
            <td align="center"><input type="text" name="oilqty" size="3" maxlength="3" /> </td>
        </tr>
        <tr>
            <td>Spark Plugs</td>
            <td align="center"><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 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><input type="submit" value="Submit Order" /></td>
        </tr>
    </table>
</form>
</body>
</html>

explain:

<form action = "processorder.php" method = "post">
</form>

form 中的action属性:

这里表示将表单数据交给"processorder.php"文件,然后在此文件中做处理

form中的method属性:

这里表示用post方法发送表单数据给"processorder.php"文件.

<td align="center"><input type="text" name="tireqty" size="3" maxlength="3" /></td>

input 中的name属性:

这里用给tires规定一个名字"tireqty"是为了在php文件中引用这个数据:

if(isset($_POST['tireqty'])){
    $tireqty = $_POST['tireqty'];
}else{
    echo "hello";

利用$_POST['tireqty']变量就可以调用从html文件中传过来的关于tires的数据,这里的isset()函数是判断变量$_POST['tireqty']是否有值,若有,则将此变量的值赋给$tireqty变量;反之,打印hello。

<td><input type="submit" value="Submit Order" /></td>

点击提交后就可将表单中填写的数据传给相应的php文件。

下面是html文件的效果图:

php完整代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<?php
//create short variable names
if(isset($_POST['tireqty'])){
    $tireqty = $_POST['tireqty'];
}else{
    echo "hello";
}
if(isset($_POST['oilqty'])){
    $oilqty = $_POST['oilqty'];
}else{
    echo "php";
}
if(isset($_POST['sparkqty'])){
    $sparkqty = $_POST['sparkqty'];
}else{
    echo "!";
}

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
if($totalqty == 0){
    echo "<p style='color:red'>";
    echo "You did not order anything on the previous page!<br />";
    echo "</p>";
}else{
    echo "<p>Your order is as follows: </p>";
    echo $tireqty." tires.<br />";
    echo $oilqty." bottles of oil.<br />";
    echo $sparkqty." spark plugs.<br />";
    echo "Items ordered: ".$totalqty."<br />";
    $totalamount = 0.00;

    define('TIREPRICE', 100);
    define('OILPRICE', 10);
    define('SPARKPRICE', 4);

    $totalamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE;

    echo "Subtotal: $".number_format($totalamount,2)."<br />";

    $taxrate = 0.10;
    $totalamount = $totalamount * (1 + $taxrate);
    echo "Total including tax: $".number_format($totalamount,2)."<br />";
}

if(isset($_POST['find'])){
    $find = $_POST['find'];
}else{
    echo "hello php!";
}

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>We do not know how this customer find us.</p>";
        break;
}
?>
</body>
</html>

explain:

define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);

这是php中定义常量的一种方法,这里定义了值为100的TIREPRICE常量、值为10的OILPRICE常量和值为4的SPARKPRICE常量。

echo "Subtotal: $".number_format($totalamount,2)."<br />";

这里使用了number_format()函数:

 

 number_format()函数使用示例:

<?php

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>
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>We do not know how this customer find us.</p>";
        break;
}

这里使用了switch选择语句。

具体效果如下: