How do I determine if the PHP check box is selected

Time:2021-9-12

Checkboxes checkboxes are usually used for PHP form submission. This article introduces how PHP determines whether the value in the check box is selected through an example. Friends who need it can refer to the examples in this article.

This article introduces you to two knowledge points:

1. How to get the value of check box in PHP form submission

2. How does PHP determine whether the value in the checkbox is selected

Let’s explain these two knowledge points respectively:

1. How does PHP get the value of the checkbox

First, let’s create a form:

<form action ="HandleFormCheckBox.php" method="post">

<ul>

 <li>< input type = "checkbox" name = "category []" value = "PHP" > PHP tutorial</li>

 <li>< input type = "checkbox" name = "category []" value = "Java" > java tutorial</li>

 <li>< input type = "checkbox" name = "category []" value = "MySQL" > MySQL tutorial</li>

 <li>< input type = "checkbox" name = "category []" value = "HTML" > HTML tutorial</li>

</ul>

<input type ="submit">

</form>

Have you found that the name attribute of all checkboxes is category [], why do you set it like this? This is because we set all checkboxes as a group, which can be used on the PHP server side$_ Post [‘category ‘] to get the values of all selected check boxes.

The code for PHP server to obtain the checkbox value is as follows:


<?php

$checkbox_select=$_POST["category"];

print_r($checkbox_select);

?>

Here’s $checkbox_ The select variable 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

Knowing how PHP gets the value of the checkbox, it will be very easy to judge whether the value in the checkbox is selected. We just need to add the variable $checkbox_ Select traversal can get which values of the checkbox are selected.

<?php

$checkbox_select=$_POST["category"];

for($i=0;$i<count($checkbox_select);$i++)

{

Echo "options". Check box_ Select [$I]. "Selected < br / >";

}

?>

The above is the details of PHP’s method to judge whether the check box is selected. If there is anything to be added, you can contact the developeppaer editor.

Recommended Today

Beautify your code VB (VBS) code formatting implementation code

However, vb.net does have many new functions that VB6 does not have. The automatic typesetting of code is one, which is the function we want to realize today – VB code formatting.Let’s look at the effect: Before formatting: Copy codeThe code is as follows: For i = 0 To WebBrowser1.Document.All.length – 1 If WebBrowser1.Document.All(i).tagName = […]