douya1855 2015-10-06 23:46
浏览 53
已采纳

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

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&ntilde;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&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>

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

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?

  • 写回答

2条回答 默认 最新

  • duanqiao3608 2015-10-06 23:52
    关注

    I suspect your issue is in this line:

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

    value=$i is missing quotes. Try value=\"$i\"

    Edit: Might I also suggest that you wrap the output in a label tag, for better usability. This way they don't have to click inside the check box, they can click the name of the topping, too. Like:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: