我正嘗試創建一個餐館訂單摘要,客戶可以通過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檢查食物和數量數組的數組長度 –