[英]How do I see which checkbox is checked?
如何检查PHP是否checkbox
了checkbox
?
If the checkbox is checked, then the checkbox's value will be passed. 如果选中该复选框,则将传递复选框的值。 Otherwise, the field is not passed in the HTTP post. 否则,该字段不会在HTTP帖子中传递。
if (isset($_POST['mycheckbox'])) {
echo "checked!";
}
you can check that by either isset()
or empty()
(its check explicit isset) weather check box is checked or not 您可以通过isset()
或empty()
(检查显式isset)检查天气复选框是否已选中
for example 例如
<input type='checkbox' name='Mary' value='2' id='checkbox' />
here you can check by 在这里你可以检查
if (isset($_POST['Mary'])) {
echo "checked!";
}
or 要么
if (!empty($_POST['Mary'])) {
echo "checked!";
}
the above will check only one if you want to do for many than you can make an array instead writing separate for all checkbox try like 以上只会检查一个,如果你想做多,你可以做一个数组而不是为所有复选框写单独的尝试
<input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
<input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
<input type="checkbox" name="formDoor[]" value="C" />Carnegie Complex<br />
php PHP
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
{
echo("You didn't select any buildings.");
}
else
{
$N = count($aDoor);
echo("You selected $N door(s): ");
for($i=0; $i < $N; $i++)
{
echo htmlspecialchars($aDoor[$i] ). " ";
}
}
Try this 试试这个
index.html 的index.html
<form action="form.php" method="post">
Do you like stackoverflow?
<input type="checkbox" name="like" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
form.php form.php的
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['like']))
{
echo "<h1>You like Stackoverflow.<h1>";
}
else
{
echo "<h1>You don't like Stackoverflow.</h1>";
}
?>
</body>
</html>
Or this 或这个
<?php
if(isset($_POST['like'])) &&
$_POST['like'] == 'Yes')
{
echo "You like Stackoverflow.";
}
else
{
echo "You don't like Stackoverflow.";
}
?>
If you don't know which checkboxes your page has (ex: if you are creating them dynamically) you can simply put a hidden field with the same name and 0 value right above the checkbox. 如果您不知道您的页面有哪些复选框(例如:如果您是动态创建它们),您只需在复选框上方放置一个名称相同且值为0的隐藏字段。
<input type="hidden" name="foo" value="0" />
<input type="checkbox" name="foo" value="1">
This way you will get 1 or 0 based on whether the checkbox is selected or not. 这样,您将根据是否选中该复选框获得1或0。
我爱短手所以:
$isChecked = isset($_POST['myCheckbox']) ? "yes" : "no";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.