2015-10-06 96 views
1

我有一个通过PHP生成的比萨菜单,其中包含一系列复选框。然后我想创建一个确认页面,列出所选的项目。我尝试使用for循环来创建披萨配料:PHP-用复选框填充表单,然后将选择发送到确认页

print("<h3>Toppings</h3>"); 
$toppings = array("Chicken", "Hamburger", "Peperoni", "Hot Sausage", "Extra Cheese", "Bell Peppers", "Jalape&ntilde;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&ntilde;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&ntilde;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循环来填充我的表单?

+0

你不需要'print()'每个html标签 –

+0

我建议在你的'$ _POST'上使用'isset',否则如果用户在没有数据的情况下访问那个页面就会中断。 – Script47

+0

@Burning晶体 - 即使它在PHP标签内? – Gned

回答

0

我怀疑你的问题是在这条线:

print ("<input type=\"checkbox\" name=\"choice[]\" value=$i> $toppings[$i]<br>");

value=$i缺少引号。尝试value=\"$i\"

编辑:也许我也建议你包的输出在label标签,以便更容易使用。这样他们不必在复选框内单击,他们也可以单击顶部的名称。像:

print ("<label><input type=\"checkbox\" name=\"choice[]\" value=$i> $toppings[$i]</label><br>");

+0

啊!这总是很愚蠢的简单。谢谢! – Gned

0

这是你如何处理多个复选框。

<?php 
$output = "<h2>Order Confirmation</h2>"; 
if (array_key_exists('choice', $_POST)) { 
    //print_r($_POST['choice']); 
    //echo '<br>'; 
    $choice = serialize($_POST['choice']);// set post array to string value 
    $choice= unserialize($choice); 
    //print_r($choice); 
    //echo '<br>'; 
    foreach($choice as $item) { 
     // echo $item . '<br>'; 
     $output .= "<li>$item</li>"; 
    } 
    $output .= '</ul>'; 
} 

我包括注释掉调试器输出,你可以用它来使它做你想做的。

此外,如别人注意,有没有需要打印或回声每一个HTML行,只是将它们连接成一个变量,调用它,比如说,$输出,然后在最后,这样做:

echo $output; 

它更加整洁,而且在服务器上更容易,速度更快。每个打印/回声基本上都会向请求的浏览器发送下一页html页面,而将它们全部放到一个变量中意味着您只输出一次。正如我们都在编程课上学到的那样,io是迄今为止大多数编程中最昂贵的部分,所以将它保持在最低限度。它也导致更可读的代码。