PHP does not automatically convert multiple checkbox information with the same name into an array like ASP does, which brings some inconvenience to use. But there is still a solution, which is to use javascript to do preprocessing. Multiple checkboxes with the same name still exist in the form of arrays in JavaScript, so before submitting the form, you can use JavaScript to combine the information in the checkboxes into a character array and assign it to the hidden elements in the form, and then use PHP The explode function parses this array, so that the check box information can be transferred. Below are examples.
Suppose there is such 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 all items. When the user clicks the Submit button, the Checker function will be called, and if the Checker returns true, the form will be submitted. If it returns false, the form will not be submitted. submit. The Checker function here is the preprocessing function we want to write. Add the following javascript in the header section of HTML:
<script language="javascript">
<!--
function Checker()
{
form1.items.value = "";
if ( !form1.item.length ) // There is only one checkbox, 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) // There is a checked box in the 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 spaces as separators form1.items.value += form1.item(j).value;
}
}
break;
}
}
}
return true;
}
-->
</script>
Use
this statement in myphp.php:
$items = explode(" ", $HTTP_POST_VARS["items"]);
These options can be separated into arrays. It should be noted that the value in the option cannot contain delimiters (here, spaces).