2

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.

4 Answers 4

0

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

1
  • Thanks for that. Your method works brilliantly. Just had to add the a foreach loop to echo out the values :) Commented Feb 23, 2014 at 10:50
0

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

Then use var_export ($ items)

0
0

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.

1
  • 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. Commented Feb 23, 2014 at 10:47
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.