How to receive check box information in PHP development

Source: Internet
Author: User
Methods for receiving check box information in PHP Development. Read the methods for receiving check box information in PHP Development. PHP does not automatically convert multiple check box information with the same name to an array as ASP does, this makes some inconvenience. But there is still a solution, that is, using javascript for preprocessing. Multiple check boxes with the same name are still in the form of arrays in javascript "> <LINKhref =" http: // w

PHP does not automatically convert multiple check boxes with the same name to an array as ASP does, which makes it inconvenient to use. But there is still a solution, that is, using javascript for preprocessing. Multiple check boxes with the same name exist in javascript as arrays. Therefore, before submitting a form, you can use javascript to combine the information groups in the check box into a character array and assign values to the hidden elements in the form, then, use the explode function in PHP to parse the array, so that the check box information can be passed. The following is an example.

Suppose there is a form:

<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 check boxes whose names are item. when you click the Submit button, the Checker function will be called, and if the Checker returns the true form, it will be submitted, if the return value is false, the form will not be submitted. Here, the Checker function is the preprocessing function to be compiled. Add the following javascript in the HTML header section:

<Script language = "javascript">
<! --
Function Checker ()
{
Form1.items. value = "";
If (! Form1.item. length) // There is only one check box, 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) // The checkbox contains the selected 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 for delimiter
Form1.items. value + = form1.item (j). value;
}
}
Break;
}
}
}
Return true;
}
-->
</Script>

In this way, you can combine the values of all selected check boxes into a string array, and use the following statement in myphp. php:

$ Items = explode ("", $ HTTP_POST_VARS ["items"]);

These options can be separated into arrays. Note that the value in the option cannot contain delimiters (here it is a space ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.