PHP Multiple Checkbox Array
Solution 1:
<form method='post' id='userform' action='thisform.php'> <tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar[]' value='Option Three'>3
</td> </tr> </table> <input type='submit' class='buttons'> </form>
<?php
if (isset($_POST['checkboxvar']))
{
print_r($_POST['checkboxvar']);
}
?>
You pass the form name as an array and then you can access all checked boxes using the var itself which would then be an array.
To echo checked options into your email you would then do this:
echo implode(',', $_POST['checkboxvar']); // change the comma to whatever separator you want
Please keep in mind you should always sanitize your input as needed.
For the record, official docs on this exist: http://php.net/manual/en/faq.html.php#faq.html.arrays
Solution 2:
add []
in the name of the attributes in the input tag
<form action="" name="frm" method="post">
<input type="checkbox" name="hobby[]" value="coding"> coding  
<input type="checkbox" name="hobby[]" value="database"> database  
<input type="checkbox" name="hobby[]" value="software engineer"> soft Engineering <br>
<input type="submit" name="submit" value="submit">
</form>
for PHP Code :
<?php
if(isset($_POST['submit']){
$hobby = $_POST['hobby'];
foreach ($hobby as $hobys=>$value) {
echo "Hobby : ".$value."<br />";
}
}
?>
Solution 3:
Try this, by for Loop
<form method="post">
<?php
for ($i=1; $i <5 ; $i++)
{
echo'<input type="checkbox" value="'.$i.'" name="checkbox[]"/>';
}
?>
<input type="submit" name="submit" class="form-control" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
$check=implode(", ", $_POST['checkbox']);
print_r($check);
}
?>
Solution 4:
You need to use the square brackets notation to have values sent as an array:
<form method='post' id='userform' action='thisform.php'>
<tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar[]' value='Option Three'>3
</td>
</tr>
</table>
<input type='submit' class='buttons'>
</form>
Please note though, that only the values of only checked checkboxes will be sent.