How to Post Multiple <Input Type="Checkbox" /> as Array in PHP

How to post multiple input type=checkbox / as array in PHP?

Do something like this:

<input type="checkbox" name="checkboxArray[]" />

Note the [] in the name.

Get $_POST from multiple checkboxes

Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).

Here's a little sample as requested:

<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
?>

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

Sending multiple checkbox options

You can send them in an array.

<form method="post">
<input type="checkbox" name="param[]" value="blue" />
<input type="checkbox" name="param[]" value="red" />
<input type="checkbox" name="param[]" value="orange" />
<input type="checkbox" name="param[]" value="green" />
<input type="checkbox" name="param[]" value="black" />
<input type="submit" value="Submit" />
</form>

>> $_POST['param']
array('blue', 'orange')

You can even use multidimensional arrays:

<form method="post">
<input type="checkbox" name="param[color][]" value="blue" />
<input type="checkbox" name="param[color][]" value="red" />
<input type="checkbox" name="param[color][]" value="orange" />
<input type="checkbox" name="param[color][]" value="green" />
<input type="checkbox" name="param[color][]" value="black" />
<input type="checkbox" name="param[year][]" value="1999" />
<input type="checkbox" name="param[year][]" value="2000" />
<input type="checkbox" name="param[year][]" value="2001" />
<input type="checkbox" name="param[year][]" value="2002" />
<input type="checkbox" name="param[year][]" value="2003" />
<input type="submit" value="Submit" />
</form>

>> $_POST['param']['color']
array('blue', 'green')

>> $_POST['param']['year']
array('2001', '2004')

Getting all selected checkboxes in an array

Formatted :

$("input:checkbox[name=type]:checked").each(function(){
yourArray.push($(this).val());
});

Hopefully, it will work.

How to submit multiple array checkbox with html forms

The problem that you are having, I suspect, is that only the checkboxes that are checked will be passed back to the server, whereas all the hidden fields will always be passed so the lengths of the arrays will differ and the keys wont correspond.

The solution to this is actually relatively simple - you just need to specify the keys for the condition array so you can match the values up again. Something like this:

HTML:

  <tr>
<td>
<input type='hidden' name='condition[100001]' value='good' />
<input type='checkbox' name='delete[]' value='100001' />
</td>
</tr>

<tr>
<td>
<input type='hidden' name='condition[100002]' value='new' />
<input type='checkbox' name='delete[]' value='100002' />
</td>
</tr>

PHP:

foreach ($_POST['delete'] as $delete) {
$condition = $_POST['condition'][$delete];
// Do stuff
}

This ties the values in the $_POST['condition'] array back up with the $_POST['delete'] array so everything will match up again.

EDIT

The way the keys are being created above is not great and in retrospect it is the wrong way to do it.

To demonstrate the right way to do it, let's imagine we have the following array, nice and simple:

$books = array(
10001 => 'good',
10002 => 'new',
10003 => 'new',
10004 => 'good'
);

What we need to do is tie up the two inputs that are associated with each book, which means we need a set of key/value pairs that can be matched up. That sounds like an array to me. But unlike the example above, the keys should be irrelevant to the data - they don't need to mean anything, because all we want is the data.

What we need to do is specify every single key explicitly (no stack-style array pushes) and make the keys agnostic of the data they relate to.

We can do this:

$i = 0;
foreach ($books as $isbn => $condition) {
echo "
<tr>
<td>
<input type='hidden' name='condition[$i]' value='$condition' />
<input type='checkbox' name='delete[$i]' value='$isbn' />
</td>
</tr>
";
$i++;
}

...and then, when the form is submitted, we can do this:

// We still base our code on $_POST['delete'] - because this is the array that
// depends on the user input. This time, though, we'll look at the keys as well
foreach ($_POST['delete'] as $key => $isbn) {
$condition = $_POST['condition'][$key];
// Do stuff
}

Getting checkbox values on submit

A good method which is a favorite of mine and for many I'm sure, is to make use of foreach which will output each color you chose, and appear on screen one underneath each other.

When it comes to using checkboxes, you kind of do not have a choice but to use foreach, and that's why you only get one value returned from your array.

Here is an example using $_GET. You can however use $_POST and would need to make both directives match in both files in order to work properly.

###HTML FORM

<form action="third.php" method="get">
Red<input type="checkbox" name="color[]" value="red">
Green<input type="checkbox" name="color[]" value="green">
Blue<input type="checkbox" name="color[]" value="blue">
Cyan<input type="checkbox" name="color[]" value="cyan">
Magenta<input type="checkbox" name="color[]" value="Magenta">
Yellow<input type="checkbox" name="color[]" value="yellow">
Black<input type="checkbox" name="color[]" value="black">
<input type="submit" value="submit">
</form>

###PHP (using $_GET) using third.php as your handler

<?php

$name = $_GET['color'];

// optional
// echo "You chose the following color(s): <br>";

foreach ($name as $color){
echo $color."<br />";
}

?>

Assuming having chosen red, green, blue and cyan as colors, will appear like this:

red

green

blue

cyan


##OPTION #2

You can also check if a color was chosen. If none are chosen, then a seperate message will appear.

<?php

$name = $_GET['color'];

if (isset($_GET['color'])) {
echo "You chose the following color(s): <br>";

foreach ($name as $color){
echo $color."<br />";
}
} else {
echo "You did not choose a color.";
}

?>

##Additional options:
To appear as a list: (<ul></ul> can be replaced by <ol></ol>)

<?php

$name = $_GET['color'];

if (isset($_GET['color'])) {
echo "You chose the following color(s): <br>";
echo "<ul>";
foreach ($name as $color){
echo "<li>" .$color."</li>";
}
echo "</ul>";
} else {
echo "You did not choose a color.";
}

?>

Make array from checkbox form

the HTML markup:

<form method="get">
<input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
<input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
<input type="checkbox" name="options[]" value="World "/> World<br/>
<input type="submit" value="Go!" />
</form>

and in the php code:

$checked = $_GET['options'];
for($i=0; $i < count($checked); $i++){
echo "Selected " . $checked[$i] . "<br/>";
}

how to use multiple if(isset($_POST)) inside php variable

First off, it should be the name attribute that gets the square bracket, not the class and this name attribute should be defined on the input checkbox not the label.

<form action="" method="post">
<input type="checkbox" name="product[]" id="product1" value="Product 1">
<label for="product1">Product 1</label>

<input type="checkbox" name="product[]" id="product2" value="Product 2">
<label for="product2">Product 2</label>

<input type="checkbox" name="product[]" id="product3" value="Product 3">
<label for="product3">Product 3</label>

<input type="submit" name="submit" class="formsubmitbtn" value="Send inquiry">
</form>

And then on the PHP side, you'll get an array of values depending on what's checked off for $_POST['product'] - you can just loop through them to get all of the items that were selected.

if(!empty($_POST['product'])) {
foreach($_POST['product'] as $p) {
echo $p;
}
}

In your email, you could do something like:

if(!empty($_POST['product'])) {
$gearselection = implode(', ', $_POST['product']);
}


Related Topics



Leave a reply



Submit