Home  >  Article  >  Backend Development  >  How to receive checkbox information in PHP development_PHP tutorial

How to receive checkbox information in PHP development_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:32:39830browse

 php(as the current mainstream development language)receiving multiple checkbox information with the same name is not automatically converted into an array like ASP, which brings some inconvenience to use. But there is still a solution, which is to 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(As the current mainstream development language) The explode function in parses this array, so that the check box information can be transferred. Below are examples.

Suppose there is such a form:

<form name="form1" id="form1" method="post" action="myphp(as the current mainstream development language ).php(as the current mainstream development language)" 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 check boxes whose names are all items. When the user clicks the Submit button , the Checker function will be called, and if Checker returns true, the form will be submitted, and if Checker 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:

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

 This way, the values ​​of all selected check boxes can be combined into a string array, in myphp
(As the current mainstream development language)
.php

(As the current mainstream development language)

Use a statement like this: $items = explode(" ", $HTTP_POST_VARS["items"]);  You can separate these options into an array. It should be noted that the value in the option cannot contain delimiters (here, spaces).

http://www.bkjia.com/PHPjc/508677.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/508677.htmlTechArticlephp (as the current mainstream development language) receiving multiple checkbox information with the same name is not automatically converted like ASP Become an array, which brings some inconvenience to use. But there is still a solution...
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