Home > Article > Backend Development > php determines whether checkbox is selected
php Determine whether the checkbox checkbox is selected
phpHow to get the value of the checkbox checkbox
First we create a form: (Recommended learning: PHP programming from entry to proficiency)
<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 names of all checkboxes are The attributes are all category[], why should they 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 "When using "java tutorial", the value of $checkbox_select is as follows:
Array( [0]='php' [1]='java' )
phpHow to determine whether the value in the checkbox checkbox is selected
Know Now that we know how to get the value of the checkbox in PHP, 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 />"; } ?>
The above is the detailed content of php determines whether checkbox is selected. For more information, please follow other related articles on the PHP Chinese website!