9

How can i get the names or id's of the multiple selected checkboxes on submit, using the PHP? Following is example form. Thanks.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
   <input type="checkbox" name="orange" id="orange">
   <input type="checkbox" name="apple" id="apple">
   <input type="checkbox" name="sky" id="sky">
   <input type="checkbox" name="sea" id="sea">
   <br>
   <br>
   <input type="submit" name="Submit" value="Submit">
</form>

4 Answers 4

30

Checkbox values are submitted from a form only if the checkbox is selected. What's more, it's the name attribute that counts, not the ID.

There are several ways of handling checkboxes in PHP:

  1. Give all checkboxes the same name followed by a pair of square brackets, so the entire set is treated as an array. In this case, give each checkbox a value.
  2. Give each checkbox a different name and a value.
  3. Give each checkbox a different name, but no value.

In each case, you need to check for the existence of the checkbox name in the $_POST array.

For example:

<input type="checkbox" name="color[]" id="orange" value="orange">
<input type="checkbox" name="color[]" id="apple" value="apple">

To get the values for these checkboxes:

if (isset($_POST['color'])) {
    $colors = $_POST['color'];
    // $colors is an array of selected values
}

However, if each checkbox has a different name and an explicit value like this:

<input type="checkbox" name="orange" id="orange" value="orange">
<input type="checkbox" name="apple" id="apple" value="apple">

You still need to use isset():

if (isset($_POST['orange'])) {
    // orange has been set and its value is "orange"
}

If you don't set a value, the default value is "on", but it won't be in the $_POST array unless it has been selected, so you still need to use isset().

Sign up to request clarification or add additional context in comments.

Comments

10

You need to give the inputs the same name:

<input type="checkbox" name="selection[]" value="orange">
<input type="checkbox" name="selection[]" value="apple">
<input type="checkbox" name="selection[]" value="sky">
<input type="checkbox" name="selection[]" value="sea">

Then iterate over the $_POST['selection'] array in PHP.

3 Comments

If you simply name them the same, this will not work. You need to set them up as arrays: name="selection[index]"
fixed the example. Also I think you can leave out the index and they will automatically be indexed.
Yes you could do that as well, he did specifically ask to preserve the "name" attribute though. The index could substitute that.
4

You won't get the ids but the names will be associative indexes in the $_POST array (and $_REQUEST). NOTE: They will only be available in the array if they were checked by the client.

if ($_POST['oragne'] == 'on')

Comments

2

You can set them up to post to PHP as arrays, if you build them similar to below:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
   <input type="checkbox" name="box_group_1[oragne]" id="oragne">
   <input type="checkbox" name="box_group_1[apple]" id="apple">
   <input type="checkbox" name="box_group_1[sky]" id="sky">
   <input type="checkbox" name="box_group_1[sea]" id="sea">
   <br>
   <br>
   <input type="submit" name="Submit" value="Submit">
</form>
<?php
print_r($_POST['box_group_1']);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.