Example of how PHP handles multiple-selection box form elements in a checkbox

Source: Internet
Author: User
Tags php form php form processing

When you recently made form submission, the check box checkbox submitted data does not know how to handle, checkbox allows multiple options, we can not each option to customize the Name property, and then the background one by one to obtain, which is not convenient to expand.

Later, after looking up some data, I found that the key point is the checkbox's Name property. You need to give the checkbox a uniform array of name, such as mycheckbox[], which is a way to define the array in PHP. The data is then retrieved in the background, such as the $_post[' MyCheckBox ' of the POST form, at which point the data is already an array. The following is a simple example.

Form form Code

<input name= "hobby[]" type= "checkbox" value= "HTML"/>html
<input name= "hobby[]" type= "checkbox" value= "CSS"/>css

PHP form Processing

Var_dump ($_post);
if (Isset ($_post["hobby"])) {
foreach ($_post["hobby"] as $hobby) {
echo "language: {$hobby}<br/>";
}
}

In fact, any set of text boxes of the same data can be used in this way to get values in batches, not just checkboxes.

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.