3

我还是 PHP 新手,所以请耐心等待。我认为我在正确的轨道上,但我无法完全弄清楚。

目标是使用单选按钮和复选框制作一个简单的订单表格。不过,它们是限制。在这种情况下,巧克力冰淇淋上没有巧克力,花生酱冰淇淋上也没有核桃。我已经尝试了将近 4 个小时来弄清楚如何让它检查一个,然后检查另一个。

我很感激任何帮助。

HTML 代码:

    <DOCTYPE xhtml>
    <head>
    <title>Lab 4 - Ice Cream</title>
    <link rel="stylesheet" type="text/css" href="lab4.css">
    </head>
    <body>
    <div id="container">
    <h3>Ice Cream Order Form</h3>
    <hr>
    <form action="lab4.php" method="post">
    Please choose a flavor:
    <ul><li><input type="radio" name="flavor" value="Vanilla" checked="checked">Vanilla</li>
    <li><input type="radio" name="flavor" value="Chocolate">Chocolate</li>
    <li><input type="radio" name="flavor" value="Peanut Butter">Peanut Butter</li>
    </ul>

    Please choose your toppings:
    <ul>
    <li><input type="checkbox" name="toppings[0]" value="Cherries">Cherries</li>
    <li><input type="checkbox" name="toppings[1]" value="Chocolate Sprinkles" id="chocsprink">Chocolate Sprinkles</li>
    <li><input type="checkbox" name="toppings[2]" value="Pineapple">Pineapple</li>
    <li><input type="checkbox" name="toppings[3]" value="Walnuts">Walnuts</li>
    <li><br /></li>
    <li><input id="submit" type="Submit" name="Submit" value="Submit"><input id="reset" type="Reset" name="Reset" value="Reset"></li>

    <div id="note">
    <span>Please note: We are unable to put chocolate sprinkles on chocolate ice cream.        </span><br />
    <span>Please note: We are unable to put walnuts on peanut butter ice cream.</span>
    </div>
    </div>
    </form>


    </body>
    </html>

PHP代码:

    $toppings=$_POST['toppings'];
    $flavor=$_POST['flavor'];
    $break='<br>';

    if (isset($_POST['toppings[1]'])) { 
        $choc = True;}

    if (isset($_POST['flavor'])==Chocolate){
        $flav = True;}

    if($choc == True && $flav === True){
    echo 'We are sorry, but you can not put chocolate sprinkles on chocolate ice cream.';}

        else {
        echo 'Thank You. <br /><br ?>';
        echo 'Your order has been placed<br /><br />';
        echo '';
        echo "You have chosen the <u><b>$flavor</b></u> ice cream with the following toppings:<br />";
    }
    if (!empty($_POST['toppings'])) {
        foreach($_POST['toppings'] as $alltoppings) {
            echo $alltoppings . $break;}}

    ?>
4

1 回答 1

4

这个:

if (isset($_POST['flavor'])==Chocolate){

是错的。isset检查该值是否已设置,并返回一个布尔值。这也会导致Chocolate一个未定义常量的警告,并且它将使用该值作为字符串。

如果要检查存在及其值,请执行以下操作:

if (isset($_POST['flavor']) && $_POST['flavor']=="Chocolate"){

除此之外(正如其他人指出的那样),您应该以不同的方式引用您的浇头:

if (isset($_POST['toppings'][1])) { 

一句忠告,启用错误报告并显示所有错误。它将帮助您制作更强大的脚本。

于 2013-10-10T07:05:16.227 回答