Question

We currently are implementing an inbox system into our website and want a user to be able to checkmark multiple inbox messages and click a Delete link to mass-delete them.

Each inbox message has it's own ID in the database.

What we need help with is getting the checkbox system working. We don't know where to start with it; whether we have to give each one an ID or fill in some value with PHP - we just don't know. Any help is greatly appreciated.

Was it helpful?

Solution

The xHTML form

<input type="checkbox" name="record[]" value="1" />
<input type="checkbox" name="record[]" value="2" />
<input type="checkbox" name="record[]" value="3" />
<input type="checkbox" name="record[]" value="4" />

The PHP

<?php

foreach ($_POST['record'] AS $id) {

    $id = (int)$id; // Force to integer (little of security)

    // Delete the record
    mysql_query("DELETE FROM `table` WHERE `id` = {$id}");

}

?>

OTHER TIPS

You can give all your check boxes the same name, ending in [], and different values like this:

<input type="checkbox" name="deletemessage[]" value="367"/>
<input type="checkbox" name="deletemessage[]" value="394"/>
<input type="checkbox" name="deletemessage[]" value="405"/>

This way, when the form is submitted, PHP will put all the selected values into an array within $_POST. So if the top and bottom check boxes were selected in the example above, $_POST['deletemessage'] would contain [367, 405]

<input type="checkbox" name="check[]" value=1 />
<input type="checkbox" name="check[]" value=2 />
<input type="checkbox" name="check[]" value=3 />
<input type="checkbox" name="check[]" value=4 />
<input type="checkbox" name="check[]" value=5 />

This will then return an array you can loop through.

<?php
foreach ($_POST["check"] as $value)
{
echo "message id: $value";
}
?>

A note on security, you will want to ensure that the message id is assosciated with the user who is trying to delete the message.

Start by giving all the check boxes the same name with square brqackets at the end somename[]

and when you submit the form (as a POST) do print_r($_POST)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top