PHP Multiple Checkbox Array

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

PHP: How to save multiple checkbox array from $_POST in variable

If you use anyname[] in the form, PHP will translate it to an array, but without the [] in the name. So this should work:

$languages = implode(',', $_POST['language']);

Note that unchecked checkboxes are not posted, so if you check none, no value is posted and $_POST['language'] will not be set. You would have to check for that.

$languages = 'No languages selected';
if (isset($_POST['language'])){
$languages = implode(',', $_POST['language']);
}

How to pass multiple checkbox values to php

Your html should look like this:

<form action="" method="post">
<input name="choice[]" type="checkbox" value="1" />
<input name="choice[]" type="checkbox" value="2" />
<input name="choice[]" type="checkbox" value="3" />
<input name="choice[]" type="checkbox" value="4" />
<input type="submit" value="order" />
</form>

In PHP, you can iterate over using foreach loop. Place the below code at the top of your html form:

A small note from basics, when posting data via post method, you need to apply Post redirect get design pattern to prevent form re-submissions.

If the data you are sending is not sensitive, you can do it via get request.

<?php
if($_POST):
foreach($_POST['choice'] as $val)
{
echo $val . "<br>";
}
endif;
?>

Create an array with multiple checkboxes

Change your HTML code like:

<p>Please check the series you watch:</p>
<form action="2.php" method="post">
<!-- make the name as an array -->
Game Of Thrones <input type="checkbox" value="Game Of Thrones" name="series[]"/><br>
The Big Bang Theory <input type="checkbox" value="The Big Bang Theory" name="series[]"/><br>
Arrow <input type="checkbox" value="Arrow" name="series[]"/><br>
<input type="submit" value="Submit">
</form>

And your send.php :

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$series = $_POST['series'];
for( $i = 0; $i < count($series); $i ++) {

$sql = "UPDATE data SET watch='yes' WHERE series='$series[$i]'";

if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
}
else {
echo "Error updating record: " . $conn->error;
}
}
$conn->close();
?>

PHP GET Form Multiple Checkbox Array 'Checked' Status from URI Parameters

First:

$checked= array();
$checked=$_GET['my-checkbox-parameter'];

And then:

<input type="checkbox" id="val1" value="val1" name="my-checkbox-parameter[]" class="form-control" <?php if(in_array("val1", $checked))echo "checked"; ?> >Value 1

So if there is val1 in your get parameters, the check box will be checked.

Do it for all check boxes and it should be ok.

AND for the second question: actually you are doing well with my-checkbox-parameter[ ], it's the usual way to do it in PHP. But you can check this question for more ways.



Related Topics



Leave a reply



Submit