Я создал этот кусок кода как часть практического упражнения, однако он не работает должным образом, я точно указал на проблему с кодом 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 < 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>
Это должно быть, if(!empty($_POST...
везде.
Восклицательный знак отсутствует, что означало бы, что он получит данные только в том случае, если поле пустое, как указано выше: «Почему вы дезинфицируете пустые данные?» от @ chris85. Следовательно, все должно иметь, если (! Empty …
кроме
if (empty($_POST["name"])) { if (empty($_POST["customeremail"])){