PHP does not automatically convert multiple check boxes with the same name to an array as ASP does, which makes it inconvenient to use. However, there is still a solution, that is, to use JavaScript for preprocessing. Multiple check boxes with the same name still exist in the form of arrays in Javascript. Therefore, before submitting a form, you can use JavaScript to combine the information groups in the check box into a number of character groups 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 like this: kaishi entertainment city
<form name="form1" id="form1" method="post" action="http://www.nowamagic.net/index.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:
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) // 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 space as the delimiter form1.items. value + = form1.item (j ). value ;}} break ;}} return true ;}
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 ).
How to Use ajax to upload an array of data