找出在php中表单中选择了哪些复选框

找出在php中表单中选择了哪些复选框,php,html,checkbox,Php,Html,Checkbox,我有一张有几个复选框的表格。当我将它提交到另一个php页面时,我想知道:如果不在每个复选框名称上调用isset,我如何知道哪些是选中的?(如果有办法的话)。如果我为每个复选框指定相同的名称,则只返回最后选中的复选框 谢谢, MichaelPHP的一个怪癖要求表单控件以字符[]结束其名称,以便识别其中多个字符 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"

我有一张有几个复选框的表格。当我将它提交到另一个php页面时,我想知道:如果不在每个复选框名称上调用isset,我如何知道哪些是选中的?(如果有办法的话)。如果我为每个复选框指定相同的名称,则只返回最后选中的复选框

谢谢,

Michael

PHP的一个怪癖要求表单控件以字符
[]
结束其名称,以便识别其中多个字符

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
   <title>Demo</title>

<form action="cb.php">
<div>
    <input type="checkbox" name="animals[]" value="dog" id="dog"> 
    <label for="dog">dog</label>
</div>
<div>
    <input type="checkbox" name="animals[]" value="cat" id="cat"> 
    <label for="cat">cat</label>
</div>
<div>
    <input type="checkbox" name="animals[]" value="rabbit" id="rabbit"> 
    <label for="rabbit">rabbit</label>
</div>
<div>
    <input type="checkbox" name="animals[]" value="hampster" id="hampster"> 
    <label for="hampster">hampster</label>
</div>
<div><input type="submit"></div>
</form>
<?php
if ($_GET['animals']) {
?>
<ul>
<?php
foreach ($_GET['animals'] as $animal) {
?>
<li><?php print htmlspecialchars($animal); ?></li>
<?php
}
?>
</ul>
<?php
}
?>

演示
狗
猫
兔子
汉普斯特

PHP的一个怪癖要求表单控件以字符
[]
结束其名称,以便识别其中的多个字符

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
   <title>Demo</title>

<form action="cb.php">
<div>
    <input type="checkbox" name="animals[]" value="dog" id="dog"> 
    <label for="dog">dog</label>
</div>
<div>
    <input type="checkbox" name="animals[]" value="cat" id="cat"> 
    <label for="cat">cat</label>
</div>
<div>
    <input type="checkbox" name="animals[]" value="rabbit" id="rabbit"> 
    <label for="rabbit">rabbit</label>
</div>
<div>
    <input type="checkbox" name="animals[]" value="hampster" id="hampster"> 
    <label for="hampster">hampster</label>
</div>
<div><input type="submit"></div>
</form>
<?php
if ($_GET['animals']) {
?>
<ul>
<?php
foreach ($_GET['animals'] as $animal) {
?>
<li><?php print htmlspecialchars($animal); ?></li>
<?php
}
?>
</ul>
<?php
}
?>

演示
狗
猫
兔子
汉普斯特

为同一组中的所有复选框使用数组名称,例如name=“mycheckbox[]”。
这样,您将获得一个数组,其中包含php代码中选定复选框的列表

为同一组中的所有复选框使用数组名称,例如name=“mycheckbox[]”。 这样,您将获得一个数组,其中包含php代码中所选复选框的列表