Skip to content
Advertisement

How to send the PHP value back to the HTML form?

I’ve been trying to build a program using PHP cookies. The user interface basically looks like this. My basic program

The logic is this. When the user enters the product name, price and quantity and clicks on the “Add” button, the total value which is price*quantity will fall on the “Total bill value” textbox. The user can go on adding more and more values for “Price” and “Quantity” and each time he clicks on “Add”, all the values get added. So this should be done by using cookies. The “Clear” button will reset the cookie, and the “Print” button will also be using the same cookie to print out everything the user has entered upto that point.

Anyways I just started coding, and I’m already stuck. I’m not sure what my mistake is. When I click on the “Add” button, the value doesn’t get added every time I enter a price and quantity. Instead the page refreshes to give only the product of the price and quantity entered at one time. This is my code.

`

    <form method="POST" >
        <ul style="list-style-type:none;">
            <li>
                Product Name: <input type="text" name="ProductName"> <br>
                Price: <input type="number" name="Price" value="price"> <br>
                Quantity: <input type="number" name="Quantity" value="qty"><br>
                <input type="submit" name="btnClick" value="Add"> 
                <input type="submit" name="btnReset" value="Clear">
                <input type="submit" name="btnPrint" value="Print"><br>
                Total Bill Value: <input type="text" name="Bill">                 
            </li>
        </ul>
    </form>


     

<?php
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $result=0;
        if(isset($_POST["btnReset"]))
        {
            setCookie("result", $result);
        }
        elseif(isset($_POST["btnClick"]))
        {
            if(isset($_COOKIE["result"]))
            {
                $result=$_COOKIE["result"];
            }
            $result= $_POST["Price"]*$_POST["Quantity"];
            echo $result; 
            setCookie("result", $result);
        }
        else
        {
            echo "Product: ".$_POST["ProductName"];
            echo "Price: ".$_POST["Price"];
            echo  "Quantity: ".$_POST["Quantity"];
        }
    }
?>
</body>
`

I’m sure that this is a stupid mistake I’m struggling on. However I am an absolute beginner and would so appreciate if somebody could point out my mistake and explain it to me. So there are two things I want to know mainly:

  1. Why is my cookie not being set when the Add button is clicked?
  2. How to put the value calculated in PHP back to the html form, so that the “Total Bill Value” textbox will have the total value?

Thanks in advance!

Edit: I did figure out my mistake. I should have put $result=$result + $_POST[“Price”]*$_POST[“Quantity”]; Then the values get added up. However would like to know how to put the total bill value calculated in PHP back to the HTML form

Advertisement

Answer

You should call setcookie() before you output any HTML, so move the PHP code above the rest.

To set the form input:

<?php 
if (isset($result)) {
    $bill = $result;
} else {
    $bill = '';
}
?>
Total Bill Value: <input type="text" name="Bill" value="<?php echo $bill; ?>">
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement