Search code examples
phphtmlpostundefined-index

index undefined php with $_POST


I am trying to get rid of the index undefined. Every time I click submit without ticking the box, I get an undefined index error.

<html>
    <head>
        <title>Order</Title>
            <style>
            </style>
        <body>
            <form action = "order.php" method = "post">
                Coffee:<p>
                <input type = "checkbox" value = "coffee" name = "cappuccino"/>Capuccino<br>
</form>
        </body> 
    </head>
</Html>

<?php
    $capuccino = 3.75;
    if(isset($_POST["submit"]))
    {
        if($_POST['cappuccino'] <> 'coffee')
        {
            $capuccino = 0;
        }
    }
?>

Solution

  • Try with isset like

    <?php
        if(isset($_POST["submit"]))
        {
            if(isset($_POST['cappuccino']) && $_POST['cappuccino'] <> 'coffee')
            {
                $capuccino = 0;
            }
        }
    ?>
    

    You can also use != instead <>

    $_POST['cappuccino'] != 'coffee'