Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a form which displays stock levels and I want the user to be able to delete multiple products so have provided checkboxes. the code is below:

echo "<form method='get'>
                        <input type='submit' name='removestock' value= 'Remove'>
                        <table class='display' border='0'>
                        <tr>
                        <th>Select</th>
                        <th>Name</th>
                        <th>Description</th>
                        <th>Price (£)</th>
                        <th>Quantity</th>
                        <th>Size</th>
                        </tr>";
                        echo "<tr>";
                        require ('connection.php');
                        $query = mysql_query("SELECT * FROM items")or die(mysql_error());
                        while($results = mysql_fetch_array($query)){
                            echo "<td> <input type='checkbox' name='item' value='".$results['item_id']."'></td>";
                            echo "<td>" . $results['name'] . "</td>";
                            echo "<td>" . $results['description'] . "</td>";
                            echo "<td>" . $results['price'] . "</td>";
                            echo "<td>" . $results['quantity'] . "</td>";
                            echo "<td>" . $results['size_id'] . "</td>";
                            echo "</tr>"; 
                        }
                        echo "</table></form>";

And my parsing code is...

if(isset($_GET['removestock']) === true){
    $errors = array();
    $items = $_GET['item'];
    echo $items;
}

But for some reason it only displays the last item_id selected. Your help will be much appreciated! PS. I tried changing the checkbox name to name="items[]" and implemented a foreach loop to parse the data but it still did not work.

share|improve this question

4 Answers 4

up vote 0 down vote accepted

The name of the checkbox should be

<input type='checkbox' name='item[]' value='".$results['item_id']."'>

This wil give you an array which you can read with a foreach

share|improve this answer
    
Thanks for that. Your method works brilliantly. Just had to add the a foreach loop to echo out the values :) –  Rumesh Feb 23 at 10:50

Use name=item [] for the checkboxes. This will give you the array.

Then use var_export ($ items)

share|improve this answer
    
Thank for your help! –  Rumesh Feb 23 at 10:50

Change this line:

"<td> <input type='checkbox' name='item' value='".$results['item_id']."'></td>";

To read:

"<td> <input type='checkbox' name='item[" . $results['item_id'] . "]'></td>";

And you will get your desired array in the variable.

share|improve this answer
    
This method worked...but the array just stores the value "on" for each checkbox that was checked. Instead, I want it to store all the "item_id"s so I can search them in the database. –  Rumesh Feb 23 at 10:47

This always works for me:

    $i =0;
    while($results = mysql_fetch_array($query)){
        echo "<td> <input type='checkbox' name='item[".$i."]' value='".$results['item_id']."'></td>";
        echo "<td>" . $results['name'] . "</td>";
        echo "<td>" . $results['description'] . "</td>";
        echo "<td>" . $results['price'] . "</td>";
        echo "<td>" . $results['quantity'] . "</td>";
        echo "<td>" . $results['size_id'] . "</td>";
        echo "</tr>";

        $i++;
    }

Once posted, the item[] element will be an array.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.