嗨,我想知道是否可以验证"checkbox"
和"number"
。真的很难解释我想要什么,但我会尽力而为;)。无论如何我有这个代码:
<input type = "checkbox" name = "coffee[]" value = "cappuccino"/>Cappuccino
<input type = "number" value = "qty." name = "cappuccino_qty" size = "2"/><br>
<input type = "checkbox" name = "coffee[]" value = "espresso"/>Espresso
<input type = "number" value = "qty." name = "espresso_qty" size = "2"/><br>
...很快
我希望程序同时验证"checkbox"
和"number"
以便用户无法作弊。例如,如果选择了“cappuccino”,则其分配的数量(“cappuccino_qty”)应该是唯一能够提交的输入类型。假设卡布奇诺咖啡成本为 2.00 美元,浓缩咖啡成本为 3.00 美元 使用我的代码,用户可以检查浓缩咖啡并将卡布奇诺的数量更改为一个或多个。因此,我的输出浓缩咖啡成本为 2.00 美元,而不是 3.00 美元。我想防止这种情况发生。
这是我的整个代码:
<html>
<head>
<title>Order</Title>
<style>
</style>
<body>
<form action = "order.php" method = "post">
Coffee:<p>
<input type = "checkbox" name = "coffee[]" value = "cappuccino"/>Cappuccino
<input type = "number" value = "qty." name = "cappuccino_qty" size = "2"/><br>
<input type = "checkbox" name = "coffee[]" value = "espresso"/>Espresso
<input type = "number" value = "qty." name = "espresso_qty" size = "2"/><br>
<input type = "checkbox" name = "coffee[]" value = "double_espresso"/>Double Espresso
<input type = "number" value = "qty." name = "double_espresso_qty" size = "2"/><br>
<input type = "checkbox" name = "coffee[]" value = "flat_white"/>Flat White
<input type = "number" value = "qty." name = "flat_white_qty" size = "2"/><br>
<input type = "checkbox" name = "coffee[]" value = "latte"/>Latte
<input type = "number" value = "qty." name = "latte_qty" size = "2"/><br>
<input type = "checkbox" name = "coffee[]" value = "ice"/>Ice Coffee
<input type = "number" value = "qty." name = "ice_qty" size = "2"/><br>
<p>
<input type = "radio" value = "in" name = "dine"/>Dine in
<input type = "radio" value = "out" name = "dine"/>Take out
<br>
<input type = "submit" value = "submit" name = "submit"/>
</form>
</body>
</head>
</Html>
<?php
//coffee cost//
$cappuccino_cost = 3.75;
$espresso_cost = 3.00;
$double_espresso_cost = 4.25;
$flat_white_cost = 3.75;
$latte_cost = 3.5;
$ice_cost = 2.5;
//default qty of each coffee//
$cappuccino_qty = 0;
$espresso_qty = 0;
$double_espresso_qty = 0;
$flatwhite_qty = 0;
$latte_qty = 0;
$ice_qty = 0;
//discounts & charges//
$charge = 1.05;
$discount = 0.1;
//submitting inputs//
if(isset($_POST["submit"]))
{
//number of checkboxe(s) that are checked
if(isset($_POST['coffee']))
{
$checked_array = $_POST['coffee'];
$count = count($checked_array);
if($count != 0)
{
//coffee cost is being readied//
if(!isset($_POST['coffee']) && $_POST['coffee'] == 'cappuccino')
{
$cappuccino_cost = 0;
}
if(!isset($_POST['coffee']) && $_POST['coffee'] == 'espresso')
{
$espresso_cost = 0;
}
if(!isset($_POST['coffee']) && $_POST['coffee'] == 'double_espresso')
{
$double_espresso_cost = 0;
}
if(!isset($_POST['coffee']) && $_POST['coffee'] == 'flat_white')
{
$flat_white_cost = 0;
}
if(!isset($_POST['coffee']) && $_POST['coffee'] == 'latte')
{
$latte_cost = 0;
}
if(!isset($_POST['coffee']) && $_POST['coffee'] == 'ice')
{
$ice_cost = 0;
}
//the quantity calculated//
if(isset($_POST['cappuccino_qty']) && $_POST['cappuccino_qty'] != 'qty.')
{
$cappuccino_qty = $_POST['cappuccino_qty'];
}
if(isset($_POST['espresso_qty']) && $_POST['espresso_qty'] != 'qty.')
{
$espresso_qty = $_POST['espresso_qty'];
}
if(isset($_POST['double_espresso_qty']) && $_POST['double_espresso_qty'] != 'qty.')
{
$double_espresso_qty = $_POST['double_espresso_qty'];
}
if(isset($_POST['flat_white_qty']) && $_POST['flat_white_qty'] != 'qty.')
{
$flat_white_qty = $_POST['flat_white_qty'];
}
if(isset($_POST['latte_qty']) && $_POST['latte_qty'] != 'qty.')
{
$latte_qty = $_POST['latte_qty'];
}
if(isset($_POST['ice_qty']) && $_POST['ice_qty'] != 'qty.')
{
$ice_qty = $_POST['ice_qty'];
}
//cost calculated//
$cappuccino = $cappuccino_cost * $cappuccino_qty;
$espresso = $espresso_cost * $espresso_qty;
$double = $double_espresso_cost * $double_espresso_qty;
$flat = $flat_white_cost * $flat_white_qty;
$latte = $latte_cost * $latte_qty;
$ice = $ice_cost * $ice_qty;
//total amount of cost and no. cofee//
$total = $cappuccino + $espresso + $double + $flat + $latte + $ice;
$total_coffee = $cappuccino_qty + $espresso_qty + $double_espresso_qty + $flat_white_qty + $latte_qty + $ice_qty;
//take away charge calculated//
if(isset($_POST['dine']) && $_POST['dine'] == 'out')
{
$total = $charge * $total;
$total = round($total,2);
}
//discount calculated//
if($count >= 3 or $total_coffee >= 3)
{
$total = $total - ($total * $discount);
$total = round($total,2);
}
//output total
if($total != 0)
{
echo "$".(round($total,2));
}
}
}
}
?>