How do I check in PHP whether a checkbox
is checked or not?
如何检查PHP是否选中了复选框?
5 个解决方案
#1
67
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!";
}
#2
29
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] ). " ";
}
}
#3
7
Try this
尝试这个
<form action="form.php" method="post">
Do you like *?
<input type="checkbox" name="like" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
<?php
if(isset($_POST['like'])
{
echo "You like *.";
}
else
{
echo "You don't like *.";
}
?>
Or this
或这个
<?php
if(isset($_POST['like']) &&
$_POST['like'] == 'Yes')
{
echo "You like *.";
}
else
{
echo "You don't like *.";
}
?>
#4
5
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。
#5
1
I love short hands so:
我爱短手所以:
$isChecked = isset($_POST['myCheckbox']) ? "yes" : "no";
#1
67
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!";
}
#2
29
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] ). " ";
}
}
#3
7
Try this
尝试这个
<form action="form.php" method="post">
Do you like *?
<input type="checkbox" name="like" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
<?php
if(isset($_POST['like'])
{
echo "You like *.";
}
else
{
echo "You don't like *.";
}
?>
Or this
或这个
<?php
if(isset($_POST['like']) &&
$_POST['like'] == 'Yes')
{
echo "You like *.";
}
else
{
echo "You don't like *.";
}
?>
#4
5
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。
#5
1
I love short hands so:
我爱短手所以:
$isChecked = isset($_POST['myCheckbox']) ? "yes" : "no";