如何读取是否在 PHP 中选中了复选框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4554758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read if a checkbox is checked in PHP?
提问by Thew
How to read if a checkbox is checked in PHP?
如何读取是否在 PHP 中选中了复选框?
回答by m_vitaly
If your HTML page looks like this:
如果您的 HTML 页面如下所示:
<input type="checkbox" name="test" value="value1">
After submitting the form you can check it with:
提交表单后,您可以通过以下方式进行检查:
isset($_POST['test'])
or
或者
if ($_POST['test'] == 'value1') ...
回答by regilero
Zend Framework use a nice hack on checkboxes, which you can also do yourself:
Zend Framework 在复选框上使用了一个不错的 hack,您也可以自己做:
Every checkbox generated is associated with a hidden field of the same name, placed just before the checkbox, and with a value of "0". Then if your checkbox as the value "1", you'll always get the '0' or '1' value in the resulting GET or POST
生成的每个复选框都与一个同名的隐藏字段相关联,位于复选框之前,值为“0”。然后,如果您的复选框为值“1”,则您将始终在结果 GET 或 POST 中获得“0”或“1”值
<input type="hidden" name="foo" value="0" />
<input type="checkbox" name="foo" value="1">
回答by rybo111
When using checkboxes as an array:
使用复选框作为数组时:
<input type="checkbox" name="food[]" value="Orange">
<input type="checkbox" name="food[]" value="Apple">
You should use in_array()
:
你应该使用in_array()
:
if(in_array('Orange', $_POST['food'])){
echo 'Orange was checked!';
}
Remember to check the array is set first, such as:
记得先检查数组是否设置,如:
if(isset($_POST['food']) && in_array(...
回答by Gautam3164
Let your html for your checkbox will be like
让您的复选框的 html 就像
<input type="checkbox" name="check1">
Then after submitting your form you need to check like
然后在提交您的表格后,您需要检查
if (isset($_POST['check1'])) {
// Checkbox is selected
} else {
// Alternate code
}
Assuming that check1
should be your checkbox name.And if your form submitting method is GET
then you need to check with $_GET
variables like
假设这check1
应该是您的复选框名称。如果您的表单提交方法是,GET
那么您需要检查$_GET
变量,例如
if (isset($_GET['check1'])) {
// Checkbox is selected
}
回答by Hammad Khan
$check_value = isset($_POST['my_checkbox_name']) ? 1 : 0;
回答by ZEESHAN ARSHAD
I've been using this trick for several years and it works perfectly without any problem for checked/unchecked checkbox status while using with PHP and Database.
我已经使用这个技巧好几年了,它在使用 PHP 和数据库时完美地工作,没有任何选中/未选中复选框状态的问题。
HTML Code: (for Add Page)
HTML 代码:(用于添加页面)
<input name="status" type="checkbox" value="1" checked>
Hint: remove "checkbox" if you want to show it as unchecked by default
提示:如果您想在默认情况下将其显示为未选中,请删除“复选框”
HTML Code: (for Edit Page)
HTML 代码:(用于编辑页面)
<input name="status" type="checkbox" value="1"
<?php if ($row['status'] == 1) { echo "checked='checked'"; } ?>>
PHP Code: (use for Add/Edit pages)
PHP 代码:(用于添加/编辑页面)
$status = $_POST['status'];
if ($status == 1) {
$status = 1;
} else {
$status = 0;
}
Hint: There will always be empty value unless user checked it. So, we already have PHP code to catch it else keep the value to 0. Then, simply use the $status variable for database.
提示:除非用户检查,否则总会有空值。所以,我们已经有了 PHP 代码来捕获它,否则将值保持为 0。然后,只需将 $status 变量用于数据库。
回答by andy
To check if a checkbox is checked use empty()
要检查复选框是否被选中,请使用empty()
When the form is submitted, the checkbox will ALWAYSbe set, because ALL POSTvariables will be sent with the form.
提交表单时,复选框将始终被设置,因为所有POST变量都将与表单一起发送。
Check if checkbox is checked with empty as followed:
检查复选框是否被选中为空,如下所示:
//Check if checkbox is checked
if(!empty($_POST['checkbox'])){
#Checkbox selected code
} else {
#Checkbox not selected code
}
回答by John Parker
You can check the corresponding value as being set and non-empty in either the $_POST or $_GET array depending on your form's action.
您可以根据表单的操作在 $_POST 或 $_GET 数组中检查相应的值是否设置为非空。
i.e.: With a POST form using a name
of "test" (i.e.: <input type="checkbox" name="test">
, you'd use:
即:使用name
“测试”的 POST 表单(即:<input type="checkbox" name="test">
,您将使用:
if(isset($_POST['test']) {
// The checkbox was enabled...
}
回答by user2451511
Learn about isset
which is a built in "function" that can be used in if statements to tell if a variable has been used or set
了解isset
哪个是内置“函数”,可以在 if 语句中使用它来判断变量是否已被使用或设置
Example:
例子:
if(isset($_POST["testvariabel"]))
{
echo "testvariabel has been set!";
}
回答by Hiram
Well, the above examples work only when you want to INSERT a value, not useful for UPDATE different values to different columns, so here is my little trick to update:
好吧,上面的例子只在你想插入一个值时才有效,对于将不同的值更新到不同的列没有用,所以这是我更新的小技巧:
//EMPTY ALL VALUES TO 0
$queryMU ='UPDATE '.$db->dbprefix().'settings SET menu_news = 0, menu_gallery = 0, menu_events = 0, menu_contact = 0';
$stmtMU = $db->prepare($queryMU);
$stmtMU->execute();
if(!empty($_POST['check_menus'])) {
foreach($_POST['check_menus'] as $checkU) {
try {
//UPDATE only the values checked
$queryMU ='UPDATE '.$db->dbprefix().'settings SET '.$checkU.'= 1';
$stmtMU = $db->prepare($queryMU);
$stmtMU->execute();
} catch(PDOException $e) {
$msg = 'Error: ' . $e->getMessage();}
}
}
<input type="checkbox" value="menu_news" name="check_menus[]" />
<input type="checkbox" value="menu_gallery" name="check_menus[]" />
....
....
The secret is just update all VALUES first (in this case to 0), and since the will only send the checked values, that means everything you get should be set to 1, so everything you get set it to 1.
秘诀就是首先更新所有 VALUES(在这种情况下为 0),并且由于只会发送检查过的值,这意味着您获得的所有内容都应设置为 1,因此您获得的所有内容都将其设置为 1。
Example is PHP but applies for everything.
示例是 PHP,但适用于一切。
Have fun :)
玩得开心 :)