PHP Multiple Checkbox Array

PhpArraysVariablesCheckbox

Php Problem Overview


I've looked around for a few examples here but a lot of them are either too advanced for my grasp of PHP or their examples are too specific to their own projects. I am currently struggling with a very basic part of a PHP form.

I am trying to create a form with a few checkboxes, each assigned a different value, I want these to be sent to a variable (array?) that I can echo/use later, in my case I will be sending the checked values in an email.

So far, I have tried a few variations, but the closest I have come to it is this...

<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
$checkboxvar[] = $_REQUEST['checkboxvar'];
?>

Where I'd echo $checkboxvar[] into my email. Am I going about this completely wrong? The other idea I had was to use a lot of if statements.

Php Solutions


Solution 1 - Php

<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 - Php

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

Solution 3 - Php

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 - Php

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.

Solution 5 - Php

Also remember you can include custom indices to the array sent to the server like this

<form method='post' id='userform' action='thisform.php'>
<tr>
	<td>Trouble Type</td>
	<td>
	<input type='checkbox' name='checkboxvar[4]' value='Option One'>4<br>
	<input type='checkbox' name='checkboxvar[6]' value='Option Two'>6<br>
	<input type='checkbox' name='checkboxvar[9]' value='Option Three'>9
	</td>
</tr>
<input type='submit' class='buttons'>
</form>

This is particularly useful when you want to use the id of individual objects in a server array accounts (for instance) to send data back to the server and recognize same at server

<form method='post' id='userform' action='thisform.php'>
<tr>
	<td>Trouble Type</td>
	<td>
    <?php foreach($accounts as $account) { ?>
	    <input type='checkbox' name='accounts[<?php echo $account->id ?>]' value='<?php echo $account->name ?>'>
        <?php echo $account->name ?>
        <br>
    <?php } ?>
	</td>
</tr>
<input type='submit' class='buttons'>
</form>

<?php 
if (isset($_POST['accounts'])) 
{
    print_r($_POST['accounts']); 
}
?>

Solution 6 - Php

if (isset($_POST['submit'])) {

for($i = 0; $i<= 3; $i++){

	if(isset($_POST['books'][$i]))

		$book .= ' '.$_POST['books'][$i];
}

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
QuestionDarek GajdaView Question on Stackoverflow
Solution 1 - Phpcryptic ツView Answer on Stackoverflow
Solution 2 - PhpRawnayView Answer on Stackoverflow
Solution 3 - PhpChaitanya BhojneView Answer on Stackoverflow
Solution 4 - PhpMartin BeanView Answer on Stackoverflow
Solution 5 - PhpUdo E.View Answer on Stackoverflow
Solution 6 - PhpSiddharth ShuklaView Answer on Stackoverflow