PHP-使用复选框填充表单,然后将选择发送到确认页面

PHP-使用复选框填充表单,然后将选择发送到确认页面,php,Php,我有一个通过PHP生成的比萨饼菜单,其中包含一系列复选框。然后我想创建一个确认页面,列出所选项目。我尝试使用for循环创建比萨配料: print("<h3>Toppings</h3>"); $toppings = array("Chicken", "Hamburger", "Peperoni", "Hot Sausage", "Extra Cheese", "Bell Peppers", "Jalape&ntilde;o Peppers", "Mushrooms

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

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>");
} 
打印(“浇头”);
$toppings=数组(“鸡肉”、“汉堡包”、“Peperoni”、“热香肠”、“额外奶酪”、“甜椒”、“墨西哥胡椒”、“蘑菇”、“橄榄”、“洋葱”);
对于($i=0;$i<$count($toppings);$i++){
打印(“$toppings[$i]
”; }
但这样的话,形式永远不会流行。因此,我使用了以下代码:

<!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>

海盗皮特比萨饼

现在,我的菜单打印得很好,但发送到我的确认页面的结果是“on”一词重复出现的次数与选中复选框的次数相同。以下是确认页面的代码:

 <!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>

订单确认


(假设我在这段时间没有把它弄糟。)


那么,如何让foreach循环发送可用信息(如字符串或索引),或者让foreach循环填充我的表单?

我怀疑您的问题在这一行:

打印($toppings[$i]

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

编辑:为了更好的可用性,我还建议您将输出包装在
标签中。这样,他们不必在复选框内单击,也可以单击浇头的名称。比如:


打印($toppings[$i]

我怀疑您的问题在这一行:

打印($toppings[$i]

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

编辑:为了更好的可用性,我还建议您将输出包装在
标签中。这样,他们不必在复选框内单击,也可以单击浇头的名称。比如:


打印($toppings[$i]

这是处理多个复选框的方式

<?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>';
}

这样做更整洁,在服务器上也更容易,速度更快。每个print/echo基本上都会将下一块页面html发送到请求的浏览器,而将其全部放在一个变量中意味着只输出一次。正如我们在编程课上所学到的,io是迄今为止大多数编程中最昂贵的部分,所以请将其降至最低。它还可以产生更可读的代码。

这就是处理多个复选框的方法

<?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>';
}

这样做更整洁,在服务器上也更容易,速度更快。每个print/echo基本上都会将下一块页面html发送到请求的浏览器,而将其全部放在一个变量中意味着只输出一次。正如我们在编程课上所学到的,io是迄今为止大多数编程中最昂贵的部分,所以请将其降至最低。它还带来了更可读的代码。

您不必对每个html标记使用
print()
,我建议您在
$\u POST
上使用
isset
,否则,如果用户访问该页面时没有数据,它就会崩溃。@Burning Crystals-即使它在PHP标记中?@Gned correct。您可以创建一个变量,如
$output
,并使用串联更新
$output
变量以保存
html
。然后,在关闭
PHP
之前,只需执行一次
print$output
@gne,如果在文件开头声明变量,代码会更干净。您不必
print()
我建议在你的
$\u帖子上使用
isset
,否则,如果用户访问该页面时没有数据,该页面将被破坏。@Burning Crystals-即使它在PHP标记中?@Gned correct。您可以创建一个变量,如
$output
,并使用串联更新
$output
变量以保存
html
。然后,在关闭
PHP
之前,只需执行一次
print$output
@gne,如果在文件开头声明变量,代码会更干净。啊!事情总是那么简单。谢谢啊!事情总是那么简单。谢谢