PHP check box selected and written to database _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags html header
PHP to check whether the check box is selected and written to the database. The following section summarizes the PHP users who need to learn whether to select and write the check box to the database. The form.html code is as follows: Copy the code formactioncheckbox. ph. below we have summarized the PHP check box selected and written to the database need to learn friends can refer.

Form.html

The code is as follows:

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

The code is as follows:

$ A = $ _ POST ["s"];
Print_r ($ );
?>

However, the above program is only used to show whether the check box is normal. If all the data in the array is retrieved one by one, a loop is required.

Therefore, the program is further modified:
Checkbox. php

The code is as follows:

$ A = $ _ POST ["s"];
For ($ I = 0; $ I {
Echo "option". $ a [$ I]. "selected
";
}
?>

The execution result is similar:
Option 3 selected
Option 15 selected

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

The code is as follows:

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:

The code is as follows:

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.