我还是 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;}}
?>