php 未选中的复选框返回空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14067215/
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
Unchecked checkbox returning null value
提问by user1809790
I have a set of checkboxes which when checked they pass the value as 1, otherwise they pass the value as 0. However, instead of sending the value of the unchecked checkboxes as '0', the value being sent is 'NULL'.
我有一组复选框,当检查它们时,它们将值传递为 1,否则它们将值传递为 0。但是,不是将未选中的复选框的值发送为“0”,而是发送的值为“NULL”。
I have the following JS code in place that should set the value to 0/1 accordingly, however still the value is sent as NULL. Is there anything that can be done to make sure that the value passed in case of an unchecked checkbox is 0?
我有以下 JS 代码,应该相应地将值设置为 0/1,但是该值仍然作为 NULL 发送。是否可以采取任何措施来确保在未选中复选框的情况下传递的值为 0?
$('#cb1, #cb2, #cb3, #cb4').on('click', function ()
$(this).val(this.checked ? 1 : 0);
});
UPDATEDHere is my html:
更新这是我的 html:
<input type="checkbox" name="cb1" id="cb1" value="1" checked />
<input type="checkbox" name="cb2" id="cb2" value="1" checked />
<input type="checkbox" name="cb3" id="cb3" value="1" checked />
<input type="checkbox" name="cb4" id="cb4" value="1" checked />
They are all checked by default.
默认情况下,它们都被选中。
If one of them is unchecked, MySQL is reporting the following error: 0SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ColumnName' cannot be null.
如果其中之一未选中,MySQL 将报告以下错误:0SQLSTATE[23000]:完整性约束违规:1048 列 'ColumnName' 不能为空。
This is because the columns are set to set not to be Null. And even though I have set the default value to 0, once I click on the submit button I still get the error. I tried to remove the Not NULL property however rather than prefilling the value as 0, the input was NULL in the database.
这是因为列被设置为不为 Null。即使我将默认值设置为 0,一旦我单击提交按钮,我仍然会收到错误消息。我试图删除 Not NULL 属性,但不是将值预填充为 0,而是数据库中的输入为 NULL。
回答by Myles Eftos
If a checkbox is unchecked, it doesn't get sent, so setting it's value to 0 if it isn't checked isn't going to help - it will always return NULL.
如果复选框未选中,则不会发送,因此如果未选中,则将其值设置为 0 无济于事 - 它始终返回 NULL。
There are two ways to fix this easily:
有两种方法可以轻松解决此问题:
1) Assume a NULL in the PHP params means the checkbox is unchecked. If the checkbox doesn't always exist on the page, this could be problematic. By the sounds of it, there is a variable number of checkboxes, so this probably won't work.
1) 假设 PHP 参数中的 NULL 表示复选框未选中。如果页面上并不总是存在该复选框,则这可能是有问题的。根据它的声音,复选框的数量是可变的,所以这可能不起作用。
2) Add a hidden input that has the same name as the checkbox with the value of 0 BEFOREthe checkbox. If the checkbox is unchecked, the hidden field value will be used, if it is checked the checkbox value will be used.
2)在复选框之前添加一个与复选框同名的隐藏输入,其值为0 。如果未选中复选框,将使用隐藏字段值,如果选中,将使用复选框值。
<input type="hidden" name="checkbox_1" value="0">
<input type="checkbox" name="checkbox_1" value="1">
Note: If your names are in an array form (ie they have square brackets in them), this won't work, as the hidden fields will increment the array count as well.
注意:如果你的名字是数组形式(即它们有方括号),这将不起作用,因为隐藏字段也会增加数组计数。
回答by Vijay Sarin
CHANGE YOUR SCRIPT LIKE THIS. HOPE IT HELPS.
像这样更改您的脚本。希望能帮助到你。
$(document).ready(function(){
$('#cb1, #cb2, #cb3, #cb4').click(function(){
$(this).val(this.checked ? 1 : 0);
});
});
回答by Arcturus
That is because if you have de-selected the checkbox, the value is null, because it has no value. Check this example from w3cschools:
那是因为如果您取消选中该复选框,则该值为 null,因为它没有任何值。从 w3cschools 查看这个例子:
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_checkbox
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_checkbox
If you have selected nothing, the value does not get submitted. But if you selected something, its value will be submitted.
如果您没有选择任何内容,则不会提交该值。但是如果你选择了一些东西,它的值就会被提交。
If you really want true/false value, you need to trick it.. so send the state of the checked attribute will do the trick.
如果你真的想要真/假值,你需要欺骗它..所以发送检查属性的状态就可以了。
$('#id:checked').length
回答by Christian
With jQuery you could use the checked selector to get the state of a checkbox. When it is checked, the length contains something greater than 0, e.G.:
使用 jQuery,您可以使用选中的选择器来获取复选框的状态。检查时,长度包含大于 0 的值,eG:
$('#id:checked').length
So you don't need to set the value of the checkbox element to 0...
所以你不需要将复选框元素的值设置为 0...
回答by sojib
You can try this , it may help you. Assume your form is like this:
你可以试试这个,它可能对你有帮助。假设你的表格是这样的:
<tr>
<td>Active</td>
<td><?php echo form_checkbox('active',set_value('active',$student->active),TRUE); ?> </td>
</tr>
Now you set your codeigniter controller like this:
现在你像这样设置你的 codeigniter 控制器:
// Set up the form
$rules = $this->student_m->rules;
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->student_m->array_from_post(array(
'active',
'name'
));
// checking is checkbox is checked or not
($data['active'] == TRUE)?($data['active'] = 1) : ($data['active'] = 0);
$this->student_m->save($data, $id);
redirect('admin/student');
}

