Checkboxcheckbox
is usually used for php form submission. This article uses an example to introduce how PHP determines whether the value in the check box is selected. Friends who need it can refer to the examples in this article.
This article introduces two knowledge points to you:
1.php form submission how to get the value of the checkbox checkbox
2.php How to determine whether the value in the checkbox is selected
Let’s explain these two knowledge points respectively:
1. How to get the value of the checkbox in PHP
First we create a form:
<form action ="HandleFormCheckBox.php" method="post"> <ul> <li><input type ="checkbox" name ="category[]" value ="php">php教程</li> <li><input type ="checkbox" name ="category[]" value ="java">java教程</li> <li><input type ="checkbox" name ="category[]" value ="mysql">mysql教程</li> <li><input type ="checkbox" name ="category[]" value ="html">html教程</li> </ul> <input type ="submit"> </form>
Have you noticed that the name attribute of all checkboxes is category[], why should it be set like this? This setting is because we treat all checkboxes as a group, and you can use $_POST['category'] on the PHP server to get the values of all selected checkboxes.
php The code for obtaining the checkbox value on the server side is as follows:
<?php $checkbox_select=$_POST["category"]; print_r($checkbox_select); ?>
The $checkbox_select variable here is an array, for example, when we select "php tutorial" and " java tutorial", the value of $checkbox_select is as follows:
Array( [0]='php' [1]='java' )
2. How does PHP determine whether the value in the checkbox is selected
Now that we know how PHP gets the value of the checkbox, it will be very simple to determine whether the value in the checkbox is selected. We only need to traverse the variable $checkbox_select to get which values in the checkbox are selected.
<?php $checkbox_select=$_POST["category"]; for($i=0;$i<count($checkbox_select);$i++) { echo "选项".$checkbox_select[$i]."被选中<br />"; } ?>
For more php related knowledge, please visit php tutorial!
The above is the detailed content of How to determine whether a checkbox is selected in php. For more information, please follow other related articles on the PHP Chinese website!