Get $_POST from multiple checkboxes

PhpHtmlFormsCheckbox

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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.
	}
}
?>

Solution 2 - Php

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
}

Solution 3 - Php

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

Solution 4 - Php

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

Solution 5 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames AndrewView Question on Stackoverflow
Solution 1 - PhpSean WalshView Answer on Stackoverflow
Solution 2 - PhpSconeView Answer on Stackoverflow
Solution 3 - PhpknittlView Answer on Stackoverflow
Solution 4 - PhpMārtiņš BriedisView Answer on Stackoverflow
Solution 5 - PhpFrantisekView Answer on Stackoverflow