在php中,接收同名的复选框的值,需要用到数组,这里分享一段代码,希望对大家有所帮助。
<html>
<head>
<title>使用默认复选框值</title>
</head>
<body>
<?php
$food = $_GET["food"];
if (!empty($food)){
echo "选择的食物有: <strong>";
foreach($food as $foodstuff){
echo '<br />'.htmlentities($foodstuff);
}
echo "</strong>.";
}
else {
echo ('
<form action="'. htmlentities($_SERVER["PHP_SELF"]).'" method="GET">
<fieldset>
<label>
Italian
<input type="checkbox" name="food[]" value="Italian" />
</label>
<label>
Mexican
<input type="checkbox" name="food[]" value="Mexican" />
</label>
<label>
Chinese
<input type="checkbox" name="food[]" value="Chinese" checked="checked" />
</label>
</fieldset>
<input type="submit" value="Go!" />
</form> ');
}
?>
</body>
</html>
2,检查复选框是否包含某些值的例子
<?php
//给定数组值
$options = array('option 1', 'option 2', 'option 3');
$valid = true;
if (is_array($_GET['input'])) {
$valid = true;
//开始匹配复选框中的选择值
foreach($_GET['input'] as $input) {
if (!in_array($input, $options)) {
$valid = false;
}
}
if ($valid) {
//process input
}
}
?>