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

时间:2016-05-14 12:52:53

标签: php html forms

我正在尝试创建一个餐厅订单摘要,客户可以通过CheckBox和数量选择订单,并在结束时获得价格总和。 当选择鸡肉时,它的工作完全正常:您选择了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>

3 个答案:

答案 0 :(得分:2)

而不是

$price = $price + 3 * $quantity[$i];

$price = $price + 8 * $quantity[$food[$i]-1];
顺便说一句,你需要将'food'中的值更改为number,当你从表单中获取它时它是一个字符串。

if(isset($_GET["choice"])){
$food=$_GET["choice"];
$quantity=$_GET["quantity"];
// begin type-cast
// without the '&',this code won't modify a array element directly
// We could iterate the array normally(without '&') and push the value to a new array,
// But that may change the order of the $quantity, I am not sure, so I'd better reference the element directly.   
foreach ($food as &$value) {
//(int) change it to a number.
    $value = (int)$value;
}
foreach ($quantity as &$value) {
    $value = (int)$value;
}
// type-cast done
$c = count($food);
$price = 0.0;


for ($i=0; $i<$c ; $i++){
if ($food[$i] == 1){
    $price = $price + 8 * $quantity[$food[$i]-1];
    echo "You have selected " .$quantity[$food[$i]-1]." Chicken <br>";
}

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

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

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

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


?>

HTML     //你这里有拼写错误          鸡,价格:8     

<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"/>

答案 1 :(得分:2)

问题是未检查的复选框不会发送到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>

答案 2 :(得分:1)

食物阵列和数量数组的长度并不总是相同。

示例:

食物数组(var_dump)

array(2) { [0]=> string(1) "2" [1]=> string(1) "3" } 

数量数组也有空值(var_dump)

array(4) { [0]=> string(0) "" [1]=> string(1) "4" [2]=> string(1) "5" [3]=> string(0) "" }

这对我有用:

if ($food[$i] == 1){
    $price = $price + 8 * $quantity[0];
    echo "You have selected " .$quantity[0]." Chicken <br>";
    continue;
}

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

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

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

希望它对你有用,如果我错了,请纠正我!