Sending all checked checkboxes to a single variable

I have a list of a large number of checkboxes, tick them off and send them to PHP. In PHP, we get each line (checkbox) of a separate variable (POST).

How can I send all checked checkboxes to one variable (for example, all value separated by commas), so that it would be easy to process?

Answer 1, authority 100%

Give a name to the checkbox name="check[0]", another name="check[1]"and so on. on the PHPside, you will get an array $_POST['check'], and accordingly $_POST['check']will have the following structure array(0 => value0, 1 => value1, ... );where value0is the value of the valueattribute of the checkbox named name="check[0]", value1– the value of the valueattribute of the checkbox named name="check[1]"etc.

$_POST[‘check’] will only contain the values ​​of checkboxes that have been checked.

Answer 2, authority 25%

To summarize the above: set the checkboxes with names of the form name="myName[]", then when submitting the form, the data will come in the form of an array $_POST[‘myName’] – this should be parsed.
If it is important for you to know which checkbox corresponds to which entry (for example, you have a list of products), use naming like name="myName[itemId]", where itemId is the id of the entry. Then the data will also come in the form of an array, but the same itemId will serve as the keys. And you will have to parse the array through foreach.

Answer 3

And what prevents you from “cleaning” the array and processing it. In $_POST, by default, 8 megabytes fit. You will have a place to turn around.


Answer 4

I had a similar task and just made numeric names for checkboxes (possible with prefixes), while in PHP I went through the entire POST array.

The following script will output the entire POST array as a table

echo "<table>";
foreach ($_POST as $key => $value){
echo "<tr><td>$key</td><td>$value</td></tr>";
echo "</table>";