Is it possible to have multiple "values" posted from a HTML form (checkboxes)?

45,573

Solution 1

So in your HTML table, you'll need to render each checkbox like this:

<input type="checkbox" name="selectedIDs[]" value="$key" />

where $key is the item number.

Then, in your PHP, you'll have a $_POST["selectedIDs"] variable, which is an array of all the item numbers that were checked.

Assuming you've got a product list array like this:

$products = array( 1 => array("Big Red", 575), 2 => array("Spearmint", 525));

You can then print a list of the selected products using a loop like this:

for($i = 0; $i < count($_POST["selectedIDs"]); $i++) {
    $key = $_POST["selectedIDs"][$i];
    $product = $products[$key];
    echo "<tr><td height='40'></td><td>" . $product[0] . "</td><td>" . $product[1] . "</td></tr>";
}

The only real difference between this and what you wrote is that my $products array is two-dimensional, and my $key is used to grab the relevant product from the $products array.

Solution 2

You can give the value of the options the value of the ID of the corresponding item. Then you'll receive an array of ID's, which you can then again map to the $br array, where you look up the name and price of an item by its ID. You could use the array key as ID, so:

<!-- Form: -->
<input type="checkbox" name="product[]" value="1" />
<input type="checkbox" name="product[]" value="..." />
<input type="checkbox" name="product[]" value="15" />
[...]

<?php
$product = array();
$product[1] = array('name' => "Big Red", 'price' => 575);
$product[...] = array('name' => "...", 'price' => ...);
$product[15] = array('name' => "Small Green", 'price' => 475);

foreach ($_POST['product'] as $pId)
{
    echo $products[$pId]['name'] . " costs " . $products[$pId]['price'];
}
?>

If of course your array originates from a database, you can use the table's ID's as values for the checkboxes. You could then implode the $_POST['product'] (of course after escaping it) with a comma, and use it in a SELECT ... WHERE ID IN ($productIds) query.

Solution 3

If i'm understanding you correctly, you'll have checkboxes something like this:

<input type="checkbox" name="product_id[]" value="1">Product #1<br/>
<input type="checkbox" name="product_id[]" value="2">Product #2<br/>
<input type="checkbox" name="product_id[]" value="3">Product #3<br/>

That should post whatever is checked as an array to your server.

Share:
45,573
Esten
Author by

Esten

Updated on April 10, 2020

Comments

  • Esten
    Esten about 4 years

    I am developing an ordering page in PHP for some products. The user needs to be able to select multiple types of products, so checkboxes will be necessary in the HTML form.

    I've established an array for the checkboxes via the "name" attribute.

    My problem is, I want to display what the user has selected on a confirmation page, so I'd like the "value" of those checkboxes to ultimately return a product name AND a product price. As far as I can tell I'm going to be stuck with one value only, so I've attempted to get around this:

    <?php
    //variables for the array and the count
    $parray = $_POST['product'];
    $pcount = count($parray);
    
    //arrays for each product's information; i've only listed one for simplicity
    $br = array("Big Red", 575);
    
    /*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/
    
    
    if (!empty($parray))
    {
        for ($i=0; $i < $pcount; $i++)
    {
        $key = $parray[i];
        echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>";
    
    }
    }
    ?>
    

    Is there anyway to make my $key variable actually act as if it is the array's name it is set to? If not, is there any good way to do this?

  • Esten
    Esten about 12 years
    Thanks a ton to you both. Very Helpful, and I appreciate it.