Home > Backend Development > PHP Tutorial > How to determine whether a checkbox is selected in php

How to determine whether a checkbox is selected in php

藏色散人
Release: 2023-04-08 09:16:01
forward
3783 people have browsed it

How to determine whether a checkbox is selected in php

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>
Copy after login

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

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

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

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!

Related labels:
php
source:cnblogs.com
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