PDA

View Full Version : Checkboxes



Sliker_Hawk
January 26th, 2006, 02:17 PM
Sorry if this has been posted before.. Just link me to it and I'll be fine.

Righty, say I have something like this:
[link to thing] [checkbox]
[link to thing] [checkbox]
And there was a link at the bottom saying 'delete selected' or some other event. Which I do, but I can't think of a way of getting the 'delete selected' to check which boxes are selected and delete the relevant things.

The number of [link to thing]s is dynamic, so I can't have the checkboxes called a number, and it won't necessarily be 1, 2, 3 etc, i.e, it could be 5, 10, 13.

Loads of CMS type things (including phpMyAdmin) have something like this, if you don't understand what I mean.

ironikart
January 26th, 2006, 04:24 PM
The trick is the naming of your checkboxes:



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


To access the checked values in php is something like this:



if (isset($_POST['checkbox']) && is_array($_POST['checkbox'])) {
foreach ($_POST['checkbox'] as $key=>$value) {
echo $value . ' checkbox has been checked.<br />';
}
}

Sliker_Hawk
January 27th, 2006, 11:01 AM
That worked perfectly, thanks!

skyler
August 1st, 2006, 04:33 AM
I spent hours on a mathematical formula that did this exact same thing. I feel like a retard.