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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀