A check box is often used in a Web page form, so how do you use PHP to determine which checkboxes in the submitted form are selected and read the data in them?
First create a form: form.html
Copy Code code as follows:
<form action=checkbox.php method=post>
<input name= "s[]" type= "checkbox" value= "3"/>3<br>
<input name= "s[]" type= "checkbox" value= "7"/>7<br>
<input name= "s[]" type= "checkbox" value= "1"/>1<br>
<input name= "s[]" type= "checkbox" value= "/>15<br>"
<input type=submit>
</form>
Then create a program that processes the form: checkbox.php
Copy Code code as follows:
<?php
$a =$_post["s"];
Print_r ($a);
?>
This can be shown in the form of an array, which is selected. Display results similar to: Array ([0]=7 [1]=15)
But the above program is only used to show whether the check box is normal, if you remove all the data in the array, you need to use a loop.
So further modify the program to: checkbox.php
Copy Code code as follows:
<?php
$a =$_post["s"];
for ($i =0; $i <count ($a); $i + +)
{
echo "Options". $a [$i]. " Be selected <br/> ";
}
?>
The results of this execution are similar to the following:
Copy Code code as follows:
Option 3 is selected
Option 15 is selected
It is more convenient to believe that the next step in the implementation of other statements.