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

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?

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>");

This is how you handle multiple checkboxes.

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

I included commented out debugger output that you can use to make it do what you want.

Also, as others noted, there's no need to print or echo out every single html line, just concatenate them into one variable, call it, say, $output, then at the end, do:

echo $output;

It's much neater that way, and also way easier on the server, and faster. Every print/echo basically shoots out the next chunk of page html to the requesting browser, whereas putting it all into one variable means you only output it once. And as we all learned in programming class, io is by far the most expensive part of most programming, so keep it to a minimum. It also leads to more readable code.