I have a pizza menu generated via PHP that includes a series of check boxes. I then want to create a confirmation page that lists the items that were selected. I tried using a for loop to create the pizza toppings:
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>");
}
But then form never populates. So I used this code instead:
<!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>
Now my menu prints fine, but the result that gets sent to my confirmation page is the word "on" repeated as many times as there are selected check boxes. Here's the code for the confirmation page:
<!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>
You can also see it loaded on a server here (assuming I haven't messed with it in the mean time.)
So, how do I either get the foreach loop to send usable information, like a string or an index#, or else get the for loop to populate my form?