检查/取消选中时如何更新数据库中的复选框值

时间:2014-11-17 12:59:41

标签: php mysql codeigniter

首先请原谅我的英语。

我正在执行codignator的申请。我希望多个复选框上的功能检查/取消选中数据库中的值必须更新。

Message :<input type="checkbox" class="Notification" name="Message"  value="1" checked="checked">
Message :<input type="checkbox" class="Notification" name="Call"  value="2" checked="checked">
Message :<input type="checkbox" class="Notification" name="Alert"  value="3" checked="checked">
<input type="submit" class="Notification" value="Submit">

现在如果我取消选中此复选框而不是数据库,则应该像

一样更新
UPDATE `user_notifications`SET `istype` = '0' WHERE notification_id=1 Or
UPDATE `user_notifications`SET `istype` = '0' WHERE notification_id=2 Or
UPDATE `user_notifications`SET `istype` = '0' WHERE notification_id=3

如果我检查所有复选框而不是数据库,它应该像

一样更新
UPDATE `user_notifications`SET `istype` = '1' WHERE notification_id=1 Or
UPDATE `user_notifications`SET `istype` = '1' WHERE notification_id=2 Or
UPDATE `user_notifications`SET `istype` = '1' WHERE notification_id=3

简单地说我有3个notification_id,每个我指定一个复选框。所以特定的复选框检查/取消选中更新查询必须要触发

请指导我如何使用模型视图控制器。

2 个答案:

答案 0 :(得分:1)

您可以查看

$post['Message'] = empty($post['Message']) ? 0 : 1;
$post['call'] = empty($post['call']) ? 0 : 1;
$post['alert'] = empty($post['alert']) ? 0 : 1;

如果检查它将给出值&#34; 1&#34;而不是它会给你&#34; 0&#34;

希望您正在寻找

答案 1 :(得分:0)

您可以使用jquery中的类名来获取选中的复选框值,如下所示

var checkedVals = $('.Notification:checkbox:checked').map(function() {
return this.value;
}).get();

警报(checkedVals.join( “”));

现在你必须使用jquery-ajax($ .post)方法来更新复选框值。