Home Backend Development PHP Tutorial 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

Jul 13, 2016 pm 05:14 PM
php under about write checkbox us database get selected

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

 代码如下 复制代码
<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" />15<br>
<input type=submit>
</form>

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

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

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

<?php

$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.
 代码如下 复制代码

<?php
$a=$_POST["s"];
for($i=0;$i<count($a);$i++)
{
echo "选项".$a[$i]."被选中<br />";
}
?>

So the program is further modified to:
checkbox.php

The code is as follows Copy code
<?php

$a=$_POST["s"];
for($i=0;$i<count($a);$i++)

{
代码如下 复制代码

<form name="form1" id="form1" method="post" action="myphp.php" onsubmit="return checker()">

<input type="checkbox" name="item" value="1">1<br>

<input type="checkbox" name="item" value="2">2<br>

<input type="checkbox" name="item" value="3">3<br>

<input type="checkbox" name="item" value="4">4<br>

<input type="hidden" name="items" value="">

<input type="submit" value="submit">

</form>

echo "option".$a[$i]."selected<br />"; } ?> 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
<form name="form1" id="form1" method="post" action="myphp.php" onsubmit="return checker()"> <input type="checkbox" name="item" value="1">1<br> <input type="checkbox" name="item" value="2">2<br> <input type="checkbox" name="item" value="3">3<br> <input type="checkbox" name="item" value="4">4<br> <input type="hidden" name="items" value=""> <input type="submit" value="submit"> </form>

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
 代码如下 复制代码

<script language="javascript">
<!--
function checker()
{
form1.items.value = "";
if ( !form1.item.length ) // 只有一个复选框,form1.item.length = undefined
{
if ( form1.items.checked )
form1.items.value = form1.item.value;
}
else
{
for ( i = 0 ; i < form1.item.length ; i++ )
{
if ( form1.item(i).checked ) // 复选框中有选中的框
{
form1.items.value = form1.item(i).value;
for ( j = i + 1 ; j < form1.item.length ; j++ )
{
if ( form1.item(j).checked )
{
form1.items.value += " "; //用空格做分割符
form1.items.value += form1.item(j).value;
}
}
break;
}
}
}
return true;
}
-->
</script>

Copy code
<script language="javascript">
<!--
function checker()
{
form1.items.value = "";
if ( !form1.item.length ) // There is only one checkbox, form1.item.length = undefined
{
if (form1.items.checked)
form1.items.value = form1.item.value;
}
else
{
for ( i = 0 ; i < form1.item.length ; i++ )
{
if ( form1.item(i).checked ) // There is a checked box in the check box
{
form1.items.value = form1.item(i).value;
for ( j = i + 1 ; j < form1.item.length ; j++ )
{
if ( form1.item(j).checked )
{
form1.items.value += " "; //Use spaces as separators
form1.items.value += form1.item(j).value;
}
}
break;
}
}
}
return true;
}
-->
</script>

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 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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles