PHP Multiple Checkbox Array PHP Multiple Checkbox Array php php

PHP Multiple Checkbox 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><?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


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 &nbsp<input type="checkbox" name="hobby[]" value="database">  database &nbsp<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 />";        }}?>


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.