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.
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 />";
}
}
?>
if (isset($_POST['submit'])) {
for($i = 0; $i<= 3; $i++){
if(isset($_POST['books'][$i]))
$book .= ' '.$_POST['books'][$i];
}
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']);
}
?>
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);
}
?>
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.
Source: Stackoverflow.com