我还是PHP的新手,所以请耐心等待。我想我已走上正轨,但我无法弄明白。
目标是使用单选按钮和复选框制作简单的订单。他们是限制。在这种情况下,巧克力冰淇淋上没有巧克力,花生酱冰淇淋上没有核桃。我现在已经尝试了近4个小时来弄清楚如何检查一个,然后检查另一个。
我感谢任何帮助。
HTML代码:
<DOCTYPE xhtml>
<head>
<title>Lab 4 - Ice Cream</title>
<link rel="stylesheet" type="text/css" href="lab4.css">
</head>
<body>
<div id="container">
<h3>Ice Cream Order Form</h3>
<hr>
<form action="lab4.php" method="post">
Please choose a flavor:
<ul><li><input type="radio" name="flavor" value="Vanilla" checked="checked">Vanilla</li>
<li><input type="radio" name="flavor" value="Chocolate">Chocolate</li>
<li><input type="radio" name="flavor" value="Peanut Butter">Peanut Butter</li>
</ul>
Please choose your toppings:
<ul>
<li><input type="checkbox" name="toppings[0]" value="Cherries">Cherries</li>
<li><input type="checkbox" name="toppings[1]" value="Chocolate Sprinkles" id="chocsprink">Chocolate Sprinkles</li>
<li><input type="checkbox" name="toppings[2]" value="Pineapple">Pineapple</li>
<li><input type="checkbox" name="toppings[3]" value="Walnuts">Walnuts</li>
<li><br /></li>
<li><input id="submit" type="Submit" name="Submit" value="Submit"><input id="reset" type="Reset" name="Reset" value="Reset"></li>
<div id="note">
<span>Please note: We are unable to put chocolate sprinkles on chocolate ice cream. </span><br />
<span>Please note: We are unable to put walnuts on peanut butter ice cream.</span>
</div>
</div>
</form>
</body>
</html>
PHP代码:
$toppings=$_POST['toppings'];
$flavor=$_POST['flavor'];
$break='<br>';
if (isset($_POST['toppings[1]'])) {
$choc = True;}
if (isset($_POST['flavor'])==Chocolate){
$flav = True;}
if($choc == True && $flav === True){
echo 'We are sorry, but you can not put chocolate sprinkles on chocolate ice cream.';}
else {
echo 'Thank You. <br /><br ?>';
echo 'Your order has been placed<br /><br />';
echo '';
echo "You have chosen the <u><b>$flavor</b></u> ice cream with the following toppings:<br />";
}
if (!empty($_POST['toppings'])) {
foreach($_POST['toppings'] as $alltoppings) {
echo $alltoppings . $break;}}
?>
答案 0 :(得分:4)
此:
if (isset($_POST['flavor'])==Chocolate){
错了。 isset
检查是否设置了值,并返回一个布尔值。这也会导致警告:Chocolate
是一个未定义的常量,并且它将使用该值作为字符串。
如果要检查存在及其值,请按以下步骤操作:
if (isset($_POST['flavor']) && $_POST['flavor']=="Chocolate"){
除此之外(正如其他人指出的那样),您应该以不同的方式引用您的浇头:
if (isset($_POST['toppings'][1])) {
建议,启用错误报告并显示所有错误。它将帮助您制作更强大的脚本。