用$ _POST索引undefined php

时间:2013-08-31 09:49:38

标签: php html post undefined-index

我试图摆脱未定义的索引。每次我点击提交时都不勾选方框,我得到一个未定义的索引错误。

<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;
        }
    }
?>

4 个答案:

答案 0 :(得分:1)

尝试isset喜欢

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

您也可以使用!=代替<>

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

答案 1 :(得分:1)

在HTML表单中,如果不进行检查,则不会发布值。你应该测试它是否是先发布的,所以你的php代码应该是这样的:

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

答案 2 :(得分:0)

你的条件应该是:

if(array_key_exists('cappuccino', $_POST) && isset($_POST['cappuccino']) && $_POST['cappuccino'] <> 'coffee')

答案 3 :(得分:0)

    <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>
<input type="submit" name="submit" value="Submit" />
</form>
        </body> 
    </head>
</Html>

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