2015-11-14 39 views
1

我創建了這段代碼作爲練習練習的一部分,但它不能按預期工作,我將問題指向PHP代碼,失去了我通過文本框輸入的值該計劃的重點是最後使用文本框中的數值和常量爲實際價格生成報價)然後它會正確輸出,但由於它已經丟失了所有值,所以我得到的是'總價格= 0'當我點擊提交時,PHP代碼丟失了它的值

任何人有任何想法? (任何意見都沒有僅僅進行錯誤校驗) 我已經包含了我在下面的鏈接完整的代碼,以避免引擎收錄錯誤

http://pastebin.com/FpDELpd8

代碼:

<!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

此一件事'!['無效<!DOCTYPE HTML!>;取下支架和這是什麼'] [1 ]'? –

+1

,你有一大堆\\一個是'$ _SERVER \ [「REQUEST_METHOD」\]'另一個'$ _POST \ [「名稱」\]'等等,他們不屬於。 –

+0

這很有趣,我剛回去檢查那些不在我的版本中 –

回答

1

應該if(!empty($_POST...無處不在。

缺少感嘆號,這意味着如果字段爲空,他將僅獲取數據,如上面的註釋「爲什麼要清理空數據?」。來自@ chris85。因此,一切都應該有,如果(空...

除了

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

這不提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你總是可以評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你會能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment)。 - [來自評論](/ review/low-quality-posts/10206903) – Lance

+0

缺少感嘆號,這意味着如果該字段爲空,他只會獲取數據,如上面的註釋「爲什麼要淨化空數據?」來自@ chris85。因此,一切應該有'如果(!空...' – Steve

+0

謝謝@RohitGupta – Steve

相關問題