我有一個通過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