php determines whether checkbox is selected

(*-*)浩
Release: 2023-02-24 15:26:01
Original
2690 people have browsed it

php Determine whether the checkbox checkbox is selected

php determines whether 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>
Copy after login

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);
?>
Copy after login

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]=&#39;php&#39; [1]=&#39;java&#39; )
Copy after login

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 />";
}
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!