Home  >  Article  >  Backend Development  >  Get whether the check box is selected in PHP and write it to the database_PHP tutorial

Get whether the check box is selected in PHP and write it to the database_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:14:131093browse

Below we summarize the information about whether the check box is selected in PHP and written to the database. Friends who need to learn can refer to it.

form.html

 代码如下 复制代码

3

7

1

15


Then create a program to process the form:
checkbox.php

The code is as follows Copy code
 代码如下 复制代码

$a=$_POST["s"];
print_r($a);
?>

$a=$_POST["s"];

print_r($a);

?>

But the above program is only used to display whether the check box is normal. If you take out all the data in the array one by one, you need to use a loop.
 代码如下 复制代码

$a=$_POST["s"];
for($i=0;$i {
echo "选项".$a[$i]."被选中
";
}
?>

So the program is further modified to:
checkbox.php

The code is as follows Copy code
$a=$_POST["s"];
for($i=0;$i {
代码如下 复制代码

1

2

3

4

echo "option".$a[$i]."selected
"; } ?> The result of this execution is similar to: Option 3 is selected Option 15 selected Use javascript to do preprocessing. Multiple checkboxes with the same name still exist in the form of arrays in javascript, so before submitting the form, you can use javascript to combine the information in the checkboxes into a character array and assign it to the hidden elements in the form, and then use php in The explode function parses this array, so that the check box information can be transferred. Below are examples. Suppose there is a form like this:
The code is as follows Copy code
1
2
3
4

This form has four checkboxes whose names are all items. When the user clicks the submit button, the checker function will be called, and if the checker returns true, the form will be submitted. If it returns false, the form will not be submitted. . The checker function here is the preprocessing function we want to write. Add the following javascript in the header section of html:

The code is as follows
 代码如下 复制代码

Copy code

http://www.bkjia.com/PHPjc/629008.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/629008.html
TechArticle
Below we summarize the information about whether the check box is selected in PHP and written to the database. Friends who need to learn can refer to it. refer to. form.html code is as follows Copy code form action=checkbox.ph...
Statement:
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