177

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.

5 Answers 5

366

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. } } ?> 
Sign up to request clarification or add additional context in comments.

10 Comments

Can you give me an example of echo(ing) one check_list[] checkbox? (And would it be similar for two selected?) Thanks.
I should also specify that if no value is set in the HTML, $check will equal on.
if($_POST) - useless, $_POST will always be true. You should check if !empty($_POST['check_list']) instead.
@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 empty() doesn't cause this error. From the manual: empty() does not generate a warning if the variable does not exist.
|
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 } 

3 Comments

Weird error: Warning: Invalid argument supplied for foreach() in /home1/mountgam/public_html/zombiewrath/reports.php on line 30 =/
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.
I tried both, but ok i'll try again (Without [] in form/name)
19

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())

Comments

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/>"; } 

Comments

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.

2 Comments

Remember that check_list[] would start with check_list[0] rather than check_list[1].
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.