我正在尝试创建一个餐厅订单摘要,客户可以通过CheckBox和数量选择他的订单,并在结束时获得价格总和.
当选择鸡肉时,它的工作完全正常:您选择了3只鸡并返回正确的价格,但是当我检查另一只并选择其他数量时它要么是
给出0选择或给出1个数量,即使我把5个例如.
只有鸡在不工作.
这是我的代码:
<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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。