我有一个通过PHP生成的比萨菜单,其中包含一系列复选框。然后我想创建一个确认页面,列出所选的项目。我尝试使用for循环来创建披萨配料:PHP-用复选框填充表单,然后将选择发送到确认页
print("<h3>Toppings</h3>");
$toppings = array("Chicken", "Hamburger", "Peperoni", "Hot Sausage", "Extra Cheese", "Bell Peppers", "Jalapeño Peppers", "Mushrooms", "Olives", "Onions");
for ($i=0; $i < $count($toppings); $i++){
print ("<input type=\"checkbox\" name=\"choice[]\" value=$i> $toppings[$i]<br>");
}
但是,然后形式永远不会填充。所以我用这个代码,而不是:
<!DOCTYPE html>
<html>
<head>
<title>Pirate Pete's Pizza</title>
</head>
<body>
<?php
include("header.php");
print("<h2>Choose the following:</h2>");
//Start form
print("<form action=\"http://csci1450.spcompsci.org/~jsword/HW/HW3/confirmation.php\" method=\"POST\">");
//Crust Selection - dropdown menu
print("<h3>Crust</h3>");
$crust = array('Hand Tossed', 'Thick Crust', 'Deep Dish', 'New York Style', 'Stuffed Crust');
print("<select name=\"crust\">");
foreach ($crust as $item1){
print ("<option>$item1</option>");
} // end foreach crust
print("</select>");
//Topping Selection - checkboxes
print("<h3>Toppings</h3>");
$toppings = array("Chicken", "Hamburger", "Peperoni", "Hot Sausage", "Extra Cheese",
"Bell Peppers", "Jalapeño Peppers", "Mushrooms", "Olives", "Onions");
for ($i=0; $i < $count($toppings); $i++){
print ("<input type=\"checkbox\" name=\"choice[]\" value=$i> $toppings[$i]<br>");
} //end for loop - toppings
?>
<br><input type="submit" value="Submit">
<input type="reset" value="Erase and Reset" id="reset">
</form>
</body>
</html>
现在我的菜单打印精细,但被发送到我的确认页的结果是“上”,因为被选中的复选框重复多次的话。下面是确认页面代码:
<!DOCTYPE HTML>
<html>
<head>
<title>Order Confirmation</title>
</head>
<body>
<?php
include("header.php");
print("<h2>Order Confirmation</h2>");
$crust = $_POST["crust"];
$choice=$_POST["choice"];
$toppings = array("Chicken", "Hamburger", "Peperoni", "Hot Sausage", "Extra Cheese",
"Bell Peppers", "Jalapeño Peppers", "Mushrooms", "Olives", "Onions");
print("You chose a $crust pizza with:<ul>");
foreach($choice as $item){
print("<li>$item</li>");
}//end foreach toppings
print("</ul>");
?>
<br><br><a href="http://csci1450.spcompsci.org/~jsword/HW/HW3/hw3.php">
<input type ="button" value="Back to Menu"><a>
</body>
</html>
你也可以把它装在服务器here上(假设我没有在平均时间与它搞砸)
那么,怎么办,我不是获得foreach循环发送可用的信息,如字符串或索引#,或者获取for循环来填充我的表单?
你不需要'print()'每个html标签 –
我建议在你的'$ _POST'上使用'isset',否则如果用户在没有数据的情况下访问那个页面就会中断。 – Script47
@Burning晶体 - 即使它在PHP标签内? – Gned