forms - How to resolve uncaught type error: unsupported operand types.... in PHP?

I created a form which works using cookies in PHP. This is how the form looks like. the form

When I put values for price and quantity, I want them to get multiplied and the result to be displayed in the "Total Bill Value" textbox. Each time I put values for price and quantity, I want them to get added up, which is why I have used a cookie. This is my code.

    <html>
    <head></head>
    <body>

    <?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= $result + $_POST["Price"]*$_POST["Quantity"];
                setcookie("result", $result);
            }
            else
            {
                if(isset($_COOKIE["result"]))
                {
                    echo "something";
                }
                
    
            }
        }
    ?>

<?php 
if (isset($result)) {
    $bill = $result;
} else {
    $bill = '';
}
?>
    
        <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" value="<?php echo $bill; ?>">                 
                </li>
            </ul>
        </form>


         
    
    
    </body>
</html>

But whenever I click the "Add" button, I'm getting this error The error

Line 21 is this

 $result= $result + $_POST["Price"]*$_POST["Quantity"];

Does anybody know why I'm getting that error??

And the other thing is, when I enter product name, price and quantity and get the total bill value and do that for many products and hit on the "Print" button, I want all the products with their total bill values to get printed out. How do I do this?

Would really appreciate any help. Thanks in advance!

Edit: So I did a var_dump to $result code

and this is what I got

var_dump($result)

Answer

Solution:

First thing I would do is move your PHP code above the opening <html> tag, this will help avoid warnings with setcookie (for example).

I've rewritten your code slightly, renaming the buttons and values. It's been tested in PHP version 5.4.45.

<?php

// Default result value
$result = 0;

if ( !empty( $_POST ) ) {

    $cookie_key = 'result';
    $action = !empty( $_POST[ 'action' ] ) ? $_POST[ 'action' ] : false;
    $result = !empty( $_COOKIE[ $cookie_key ] ) ? (float)$_COOKIE[ $cookie_key ] : $result;


    // Add product
    if ( $action === 'add' ) {
        $price = !empty( $_POST[ 'Price' ] ) ? (float)$_POST[ 'Price' ] : 0;
        $quantity = !empty( $_POST[ 'Quantity' ] ) ? (int)$_POST[ 'Quantity' ] : 0;
        if ( $price && $quantity ) {
            $result = $result + ( $price * $quantity );
        }
    }

    // Clear result
    else if ( $action === 'clear' ) {
        $result = 0;
    }

    // Print
    else if ( $action === 'print' ) {
        // Handle print action
    }

    // Store value
    setcookie( $cookie_key, $result );
}
?>
<html>
<head></head>
<body>
    <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="action" value="add"> 
                <input type="submit" name="action" value="clear">
                <input type="submit" name="action" value="print"><br>
                Total Bill Value: <input type="text" name="Bill" value="<?php echo $result; ?>">
            </li>
        </ul>
    </form>

</body>
</html>

Here it is in action:

enter image description here

Source