T.Somers
T.Somers

Reputation: 21

PHP Code losing its values when I hit submit

I created this piece of code as part of a practise exercise, however it is not working as intended, I pinpointed the issue to the PHP code losing the values I input via text boxes (The point of the program is to generate a quote for product prices at the end using values from text boxes for the quantities and constants for the actual prices) It then outputs correctly but since it has lost the values all I get is 'Total price = 0'

Anyone got any ideas? (any comments are there merely for error checking) I have included my full code in the link below to avoid errors

http://pastebin.com/FpDELpd8

Code from 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>

Upvotes: 1

Views: 100

Answers (1)

Steve
Steve

Reputation: 818

It should be if(!empty($_POST... everywhere.

The exclamation mark is missing, which would mean that he would only get data if the field were empty, as per the comment above "why are you sanitizing empty data?" from @chris85. Hence everything should have if(!empty...

except for

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

Upvotes: 1

Related Questions