php - HTML PHP 添加数量和价格到我的菜单项

标签 php html forms

我正在尝试创建一个餐厅订单摘要,客户可以在其中通过复选框和数量选择他的订单,并在最后获得价格总和。 选择鸡肉时,它的工作正常很好:您选择了3只鸡并返回正确的价格,但是当我检查另一只鸡肉并选择其他数量时,它是 给出 0 个选择或给出 1 个数量,即使我放 5 个。 只有鸡在工作,其他的则不然。

查看我的代码和带输出示例的屏幕截图。 Test 2 should give: you have selected 2 Meat Total 6

这是我的代码:

<body>
<h3>Select what you want to eat</h3>

<form action="PlaceOrder.php" method="get"/>
<input type="checkbox" name="choice[]" value="1"/>Chicken,Price:8
<input name="quantity[]" type="text" /><br />

<input type="checkbox" name="choice[]" value="2"/>Meat,Price:3
<input name="quantity[]" type="text" /><br />

<input type="checkbox" name="choice[]" value="3"/>Souvlaki,Price:2.50
<input name="quantity[]" type="text" /><br />

<input type="checkbox" name="choice[]" value="4"/>Pizza,Price:12
<input name="quantity[]" type="text" /><br />

<input type="submit" value="Order"/>
</form>

和 php:

<?php
if(isset($_GET["choice"])){
$food=$_GET["choice"];
$quantity=$_GET["quantity"];
$c = count($food);
$price = 0.0;


for ($i=0; $i<$c ; $i++){
    if ($food[$i] == 1){
        $price = $price + 8 * $quantity[$i];
//here it's not working with quantity
        echo "You have selected " .$quantity[$i]." Chicken <br>";
    }

    if ($food[$i] == 2){
        $price = $price + 3 * $quantity[$i];
        echo "You have selected" .$quantity[$i]." Meat <br>";
    }

    if ($food[$i] == 3){
        $price = $price + 2.5 * $quantity[$i];
        echo "You have selected " .$quantity[$i]."Souvlaki <br>";
    }

    if ($food[$i] == 4){
        $price = $price + 12 * $quantity[$i];
        echo "You have selected" .$quantity[$i]." Pizza <br>";
    }       
   }

  echo "Total: ".$price . "<br>";
}
else {
echo "Please select something!";
}


?>




</body>

最佳答案

问题是未选中的复选框不会发送到 PHP,因此您的代码不会像您在那里的那样工作。您需要在表单中包含有关每个项目的更多信息,以便在 PHP 端更轻松地处理数据,而且这样您就不必在显示项目名称时重复自己。尝试这样的事情:

<?php
$total = 0;
$items = [];
$info  = 'Select something to order.';

// form submitted
if( !empty( $_POST['choice'] ) && is_array( $_POST['choice'] ) )
{
    // loop all item choices
    foreach( $_POST['choice'] as $item )
    {
        // filter item info
        $name     = trim( $item['name'] );
        $price    = floatval( $item['price'] );
        $quantity = intval( $item['quantity'] );

        // only add if item was checked and quantity is more than 0
        if( isset( $item['checked'] ) && $quantity > 0 )
        {
            $items[] = $quantity .' '. $name;
            $total  += $price * $quantity;
        }
    }
    // update info if items were selected
    if( count( $items ) )
    {
        $info = 'You selected ('.implode( ', ', $items ).'), total: '.$total;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Order Form</title>
    <meta charset="utf-8" />
</head>
<body>

    <form id="order-form" action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
        <div class="form-item">
            <input type="checkbox" name="choice[0][checked]" />
            <span>Chicken, Price: 8</span>
            <input type="number" name="choice[0][quantity]" value="1" />
            <input type="hidden" name="choice[0][price]" value="8" />
            <input type="hidden" name="choice[0][name]" value="Chicken" />
        </div>
        <div class="form-item">
            <input type="checkbox" name="choice[1][checked]" />
            <span>Meat, Price: 3</span>
            <input type="number" name="choice[1][quantity]" value="1" />
            <input type="hidden" name="choice[1][price]" value="3" />
            <input type="hidden" name="choice[1][name]" value="Meat" />
        </div>
        <div class="form-item">
            <input type="checkbox" name="choice[2][checked]" />
            <span>Souvlaki, Price: 2.50</span>
            <input type="number" name="choice[2][quantity]" value="1" />
            <input type="hidden" name="choice[2][price]" value="2.50" />
            <input type="hidden" name="choice[2][name]" value="Souvlaki" />
        </div>
        <div class="form-item">
            <input type="checkbox" name="choice[3][checked]" />
            <span>Pizza, Price: 12</span>
            <input type="number" name="choice[3][quantity]" value="1" />
            <input type="hidden" name="choice[3][price]" value="12" />
            <input type="hidden" name="choice[3][name]" value="Pizza" />
        </div>
        <input type="submit" value="Order"/>
    </form>

    <hr />
    <p><?= $info ?></p>

</body>
</html>

关于php - HTML PHP 添加数量和价格到我的菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37226912/

相关文章:

php - 在 Woocommerce Admin Orders 列表中添加可排序的自定义列

html - 注册流程栏CSS3动画

javascript - 全日历 Rails7

python - Django Form Clean 方法测试错误

php - php-fpm 容器中的用户 session 量如何?

javascript - 添加/编辑网页的地址栏,而不删除已有的内容。

PHP 5.4 和 SMTP

javascript - 默认情况下选中复选框时,即时更改行的背景颜色

forms - 基于 Tomcat FORM 的身份验证,在每个页面上

javascript - 获取使用 Javascript 添加的表单字段