当我点击提交时,PHP代码会丢失其值

时间:2015-11-14 17:45:20

标签: php

我创建了这段代码作为练习练习的一部分,但它没有按预期工作,我向PHP代码指出了丢失我通过文本框输入的值的问题(程序的要点是生成一个产品价格最后使用文本框中的值来表示实际价格的数量和常数。然后输出正确,但由于它丢失了所有值,我得到的是“总价格= 0”

有人有任何想法吗? (任何评论仅用于错误检查) 我已在下面的链接中包含我的完整代码以避免错误

http://pastebin.com/FpDELpd8

来自pastebin的代码:

<!DOCTYPE HTML>
<html>
        <head>Validation example
        </head>
<body>
<?php
        //?
        $nameErr = $CustomeremailErr = $MonitorqtyErr = $PCqtyErr = $MiceqtyErr = $KeyboardqtyErr = $SpeakerqtyErr = "";
        $PCqty = $Miceqty = $Keyboardqty = $Monitorqty = $Speakerqty = 0;
        $name = $Customeremail = "";
        DEFINE ("PCPRICE" , 300.01);
        DEFINE ("MONITORPRICE" , 100);
        DEFINE ("KEYBOARDPRICE" , 5);
        DEFINE ("MICEPRICE" , 5);
        DEFINE ("SPEAKERPRICE" , 20);
        DEFINE ("DODGYDELIVERY", 35.05);


        //?
        if ($_SERVER["REQUEST_METHOD"] == "POST") {

                if (empty($_POST["name"])) {
                        $nameErr = "Name is required";
                } else {
                        $name = clean_input($_POST["name"]);
                        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                                $nameErr = "Only letters and white space allowed";
                        }
                }
                if (empty($_POST["customeremail"])) {
                        $customeremailErr = "Email is required";
                } else {
                        $Customeremail = clean_input($_POST["customeremail"]);
                        //check if email is well-formed
                         if (!filter_var($Customeremail, FILTER_VALIDATE_EMAIL)) {
                                $CustomeremailErr = "Invalid email format";
                        }
                }
                if (empty($_POST["PCqty"])) {
                        $PCqty = clean_input($_POST["PCqty"]);
                } else if (preg_match("/^[a-zA-Z ]*$/",$PCqty)) {
                        $PCqtyErr = "Only Numbers Allowed";
                }
                        if (empty($_POST["Monitorqty"])) {
                        $Monitorqty = clean_input($_POST["Monitorqty"]);
                } else if (preg_match("/^[a-zA-Z ]*$/",$Monitorqty)) {
                        $MonitorqtyErr = "Only Numbers Allowed";
                }

                if (empty($_POST["Keyboardqty"])) {
                        $Keyboardqty = clean_input($_POST["Keyboardqty"]);
                        } else if (preg_match("/^[a-zA-Z ]*$/",$Keyboardqty)) {
                        $KeyboardqtyErr = "Only Numbers Allowed";
                        }
                if (empty($_POST["Miceqty"])) {
                                $Miceqty = clean_input($_POST["Miceqty"]);
                        } else if (preg_match("/^[a-zA-Z ]*$/",$Miceqty)) {
                        $MiceqtyErr = "Only Numbers Allowed";
                        }
                //?Fix change to speaker
                if (empty($_POST["Speakerqty"])) {
                        $Speakerqty = clean_input($_POST["Speakerqty"]);
                } else if (preg_match("/^[a-zA-Z ]*$/",$Speakerqty)) {
                        $SpeakerqtyErr = "Only Numbers Allowed";
                }
        }

        // function test_input cleans up data by trimming off blank space, removing slashes and
        //coding characters like < to &lt to prevent hacking
        function clean_input ($data) {
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
        }
        ?>

        <h2>PHP Form Validation Example</h2>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">


        Name: <input type="text" name="name" value="<?php echo $name;?>">
        <span class="error">*<?php echo $nameErr;?></span>
        <br><br>
        email: <input type="text" name = "customeremail" value="<?php echo $Customeremail;?>">
        <span class="error">*<?php echo $CustomeremailErr;?></span>
        <br><br>
        PC quantity: <input type="text" name="PCqty" value="<?php echo $PCqty;?>">
        <span class="error">*<?php echo $PCqtyErr;?></span>
        <br><br>
        Keyboard quantity: <input type="text" name="Keyboardqty" value="<?php echo $Keyboardqty;?>">
        <span class="error">*<?php echo $KeyboardqtyErr;?></span>
        <br><br>
        Mice Quantity: <input type="text" name="Miceqty" value="<?php echo $Miceqty;?>">
        <span class="error">*<?php echo $MiceqtyErr;?></span>
        <br><br>
        Speaker Quantity: <input type="text" name="Speakerqty" value="<?php echo $Speakerqty;?>">
        <span class="error">*<?php echo $SpeakerqtyErr;?></span>
        <br><br>
        Monitor Quantity: <input type="text" name="Monitorqty" value="<?php echo $Monitorqty;?>">
        <span class="error">*<?php echo $MonitorqtyErr;?></span>
        <br><br>

        <input type="submit" name="submit" value="Submit">
        </form>
<?php

$PcTotalCost = $SpeakerTotalCost = $MonitorTotalCost = $MiceTotalCost = $KeyboardTotalCost = 0 ;
// Has this form been submitted and was data entered then do the calculations
        if ($_SERVER["REQUEST_METHOD"] == "POST") {

        // check there are no errors in the entries

        if (($nameErr == "") && ($CustomeremailErr == "") && ($PCqtyErr == "") && ($MonitorqtyErr == "") && ($MiceqtyErr == "") && ($KeyboardqtyErr == "") && ($SpeakerqtyErr == "") )
         {

                        echo "<h2>Quote</h2>";
                        // Do required calculation
                         $PcTotalCost = $PCqty * PCPRICE;
                        $KeyboardTotalCost = $Keyboardqty * KEYBOARDPRICE;
                        $MonitorTotalCost = $Monitorqty * MONITORPRICE;
                        $SpeakerTotalCost = $Speakerqty * SPEAKERPRICE;
                        $MiceTotalCost = $Miceqty * MICEPRICE;
                        $GoodsTotalCost = $MiceTotalCost + $SpeakerTotalCost + $MonitorTotalCost + $KeyboardTotalCost + $PcTotalCost;
                        $VatRate = $GoodsTotalCost / 100;
                        $GrossCost = $GoodsTotalCost + $VatRate + DODGYDELIVERY;
                        // Displaying the result
                                echo 'Dodgy Dan’s quote for ' . $name . ' on ' . date(1) . "<br/>" ;
                                echo $PCqty . " PC's at a cost of £ ". number_format(PCPRICE, 2) . " each  would cost £ " . number_format($PcTotalCost, 2) . "<br/>";
                                echo $Keyboardqty . " Keyboards at a cost of £ ". number_format(KEYBOARDPRICE, 2) . " each  would cost £ " . number_format($KeyboardTotalCost, 2) . "<br/>";
                                echo $Miceqty . " Mice at a cost of £ ". number_format(MICEPRICE, 2) . " each  would cost £ " . number_format($MiceTotalCost, 2) . "<br/>";
                                echo $Speakerqty . " Speakers at a cost of £ ". number_format(SPEAKERPRICE, 2) . " each  would cost £ " . number_format($SpeakerTotalCost, 2) . "<br/>";     
                                echo $Monitorqty . " Monitors at a cost of £ ". number_format(MONITORPRICE, 2) . " each  would cost £ " . number_format($MonitorTotalCost, 2) . "<br/>";
                                echo " The Delivery charge would be £" . number_format (DODGYDELIVERY, 2) . "<br/>" ;
                                echo ' The total cost of goods is £' . $GoodsTotalCost . "<br/>" ;
                                echo " The VAT(10%)rate for this purchase is £" . $VatRate . "<br/>" ;
                                echo ' The total gross cost after VAT for this order is £' . $GrossCost . "<br/>" ;


                }
        }
?>
        </body>
        </html>

1 个答案:

答案 0 :(得分:1)

到处都应该if(!empty($_POST...

感叹号缺失,这意味着如果字段为空,他只会获取数据,根据上面的评论“为什么要清理空数据?”来自@ chris85。因此,一切都应该有(!空......

除了

     if (empty($_POST["name"])) {
     if (empty($_POST["customeremail"])){