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>
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:
In each case, you need to check for the existence of the checkbox name in the $_POST array.
For example:
To get the values for these checkboxes:
However, if each checkbox has a different name and an explicit value like this:
You still need to use isset():
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().
You can set them up to post to PHP as arrays, if you build them similar to below:
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')
You need to give the inputs the same name:
Then iterate over the $_POST['selection'] array in PHP.