176

I have 1 form in with multiple checkboxes in it (each with the code):

<input type="checkbox" name="check_list" value="<? echo $row['Report ID'] ?>">

Where $row['Report ID'] is a primary key in a database -so each value is different.

How would I be able to tell which checkboxes have been checked? (Maybe multiple)

This is for an inbox system and I have a button below that I want (when clicked) to delete all messages (ids of: $row['Report ID']) which have the checkbox's checked.

halfer
  • 19,824
  • 17
  • 99
  • 186
James Andrew
  • 7,197
  • 14
  • 46
  • 62

5 Answers5

363

Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).

Here's a little sample as requested:

<form action="test.php" method="post">
    <input type="checkbox" name="check_list[]" value="value 1">
    <input type="checkbox" name="check_list[]" value="value 2">
    <input type="checkbox" name="check_list[]" value="value 3">
    <input type="checkbox" name="check_list[]" value="value 4">
    <input type="checkbox" name="check_list[]" value="value 5">
    <input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {
            echo $check; //echoes the value set in the HTML form for each checked checkbox.
                         //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                         //in your case, it would echo whatever $row['Report ID'] is equivalent to.
    }
}
?>
ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30
Sean Walsh
  • 8,266
  • 3
  • 30
  • 38
  • 4
    Can you give me an example of echo(ing) one check_list[] checkbox? (And would it be similar for two selected?) Thanks. – James Andrew Feb 14 '11 at 21:09
  • 2
    I should also specify that if no value is set in the HTML, `$check` will equal `on`. – Sean Walsh Feb 14 '11 at 21:26
  • 9
    `if($_POST)` - useless, `$_POST` will always be true. You should check if `!empty($_POST['check_list'])` instead. – Mārtiņš Briedis Feb 14 '11 at 23:08
  • 3
    @MārtiņšBriedis that would cause an array index out of bounds if it didn't exist. A better check would be `array_key_exists('check_list', $_POST) && !empty($_POST['check_list'])` – Tyzoid Aug 01 '13 at 20:13
  • 2
    @Tyzoid `empty()` doesn't cause this error. From the manual: `empty() does not generate a warning if the variable does not exist. ` – Mārtiņš Briedis Aug 02 '13 at 07:07
  • 1
    @Sean can you help me with this same case. In my case, I want to make the user ONLY be able to choose one of the multiple checkbox, so the data that will be send to the database is only one. Thanks – Al Kush Nov 02 '14 at 10:20
  • 1
    @AlKush You should use a radio input if you want to limit the user to only one choice. – Sean Walsh Nov 02 '14 at 22:58
  • To filter this array value, see the comment by _rimelek_ in the [PHP manual entry for the filter_input() function](http://php.net/manual/en/function.filter-input.php) — in brief: use the filter option, **FILTER_REQUIRE_ARRAY** – Matthew Slyman Dec 10 '15 at 19:02
  • `$_POST['check_list']` will not even be set if none of the checkboxes are checked. – kjdion84 May 21 '18 at 22:33
  • @kjdion84 you can use `if (isset($_POST['check_list']))` to check for that. – majick Jul 05 '18 at 01:08
24

Edit To reflect what @Marc said in the comment below.

You can do a loop through all the posted values.

HTML:

<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />

PHP:

foreach($_POST['check_list'] as $item){
  // query to delete where item = $item
}
Nikunj Madhogaria
  • 2,139
  • 2
  • 23
  • 40
Scone
  • 669
  • 6
  • 11
  • 1
    Weird error: Warning: Invalid argument supplied for foreach() in /home1/mountgam/public_html/zombiewrath/reports.php on line 30 =/ – James Andrew Feb 14 '11 at 21:13
  • 2
    That would only work if you use the `[]` syntax in the field definition, which makes PHP create that $_POST value as an array. Otherwise it'll be a single non-array value, causing the foreach() loop to blow up. – Marc B Feb 14 '11 at 21:20
  • 1
    I tried both, but ok i'll try again (Without [] in form/name) – James Andrew Feb 14 '11 at 21:21
18

you have to name your checkboxes accordingly:

<input type="checkbox" name="check_list[]" value="…" />

you can then access all checked checkboxes with

// loop over checked checkboxes
foreach($_POST['check_list'] as $checkbox) {
   // do something
}

ps. make sure to properly escape your output (htmlspecialchars())

knittl
  • 246,190
  • 53
  • 318
  • 364
14
<input type="checkbox" name="check_list[<? echo $row['Report ID'] ?>]" value="<? echo $row['Report ID'] ?>">

And after the post, you can loop through them:

   if(!empty($_POST['check_list'])){
     foreach($_POST['check_list'] as $report_id){
        echo "$report_id was checked! ";
     }
   }

Or get a certain value posted from previous page:

if(isset($_POST['check_list'][$report_id])){
  echo $report_id . " was checked!<br/>";
}
Mikeys4u
  • 1,494
  • 18
  • 26
Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76
6

It's pretty simple. Pay attention and you'll get it right away! :)

You will create a html array, which will be then sent to php array. Your html code will look like this:

<input type="checkbox" name="check_list[1]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[2]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[3]" alt="Checkbox" value="checked">

Where [1] [2] [3] are the IDs of your messages, meaning that you will echo your $row['Report ID'] in their place.

Then, when you submit the form, your PHP array will look like this:

print_r($check_list)

[1] => checked [3] => checked

Depending on which were checked and which were not.

I'm sure you can continue from this point forward.

Thusitha Sumanadasa
  • 1,669
  • 2
  • 22
  • 30
Frantisek
  • 7,485
  • 15
  • 59
  • 102
  • 7
    Remember that `check_list[]` would start with `check_list[0]` rather than `check_list[1]`. – Arbiter May 28 '14 at 08:01
  • 2
    value="checked" is counter-productive. If no value is specified you get 'on'. The advantage of specifying the values you want to submit in the value attributes is that you can send almost arbitrary data that doesn't have to become a PHP array index. Iterating over array values with foreach is also easier than iterating over the keys. – Rudiger W. Jan 25 '16 at 08:44