我正尝试创建一个餐馆订单摘要,客户可以通过CheckBox和数量选择他的订单,并在最后获得价格总和。 当选择鸡的工作完美的前例:您选择了3只鸡,并返回正确的价格,但是当我检查另一个并选择其他数量时,它是 给0选择或给1数量,即使我把5例如。 只有鸡在别人工作。HTML PHP将数量和价格添加到我的菜单项
这是我的代码:
<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>
请问你能更具体一些,并给出更多的例子说明?如果可能的话附上评论谢谢。 – Johnny
在错误情况下使用var dump检查食物和数量数组的数组长度 –