Get $_Post from Multiple Checkboxes

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.
}
}
?>

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;
?>

Issue getting $_POST from multiple checkboxes

Ok, problem found.
In a previous part of the code, there was an include of a config file, where the following code generate the problem:

if (!get_magic_quotes_gpc()) {
if (isset($_POST)) {
foreach ($_POST as $key => $value) {
$_POST[$key] = trim(addslashes($value));
}
}

if (isset($_GET)) {
foreach ($_GET as $key => $value) {
$_GET[$key] = trim(addslashes($value));
}
}
}

Removing it, the form works!

Get POST data from multiple checkboxes?

Name the fields like service[] instead of service, then you'll be able to access it as array. After that, you can apply regular functions to arrays:

  • Check if a certain value was selected:

     if (in_array("Other", $_POST['service'])) { /* Other was selected */}
  • Get a single newline-separated string with all selected options:

     echo implode("\n", $_POST['service']);
  • Loop through all selected checkboxes:

     foreach ($_POST['service'] as $service) {
    echo "You selected: $service <br>";
    }

Assign post values of multiple checkbox inputs to a PHP variable and separate by comma

Give name to checkbox as an array, so when you submit form you will get an array of checkbox values in the array which checkboxes are checked then you can implode that array with , comma separated.

<input type="checkbox" name="checkboxname[]" value="1" checked>Google
<input type="checkbox" name="checkboxname[]" value="2">Amazon
<input type="checkbox" name="checkboxname[]" value="3" checked>Microsoft

After submitting form you have to get values like below code.

$data = implode(",", $_POST['checkboxname']);
echo $data;

How to get the value of multiple checkbox using php

You are recieving the undefined index notice because the index 'twitter' of the $_POST array isn't declared (assuming the form is using post). If the form uses get the twitter array would be initialized as an index of $_GET. As other answers suggest, check if that index is set using the php isset method

<?php
If(isset ($_POST ['twitter'] )) {
// your code here
}
?>

Sending Multiple Checkbox Values in Form Post Request

Change name="position" to name="position[]" in all of your input tags and then handle the $_POST['position'] variable as an array.

Use var_dump($_POST) to see what data will be sent to PHP after you fix the name attributes in your HTML.



Related Topics



Leave a reply



Submit