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