如何使用jquery检查复选框时更新数据库? [英] How to update Database while checking checkbox with jquery?

查看:76
本文介绍了如何使用jquery检查复选框时更新数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jquerys $ .post或$ .ajax检查复选框(1或0)时更新数据库中的值。我真的很感激一些帮助。这是我的代码:

I am trying to update a value in DB while checking checkbox (1 or 0) with jquerys $.post or $.ajax. I would really appreciate some help. Here is my code:

HTML

<div class="onoffswitch">
     <?php
    $yesno = (bool)$baner['noti'];
    $checked = ($yesno) ? 'checked="checked"' : '';  
     ?>
   <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" value="1" id="myonoffswitch" <?php echo $checked; ?>>
   <label class="onoffswitch-label" for="myonoffswitch"></label>
</div>

jQuery

$(function(){
    $("#myonoffswitch").click(function(){
        $.post("notification.php");
        })
    });

notification.php

connections stuffs...

$yesno = ( isset($_POST['onoffswitch']) ) ? 1 : 0;
$sql = "UPDATE baner SET noti='$yesno'";


推荐答案

你几乎拥有它。您只需要阅读您的复选框状态并将其与POST请求一起发回。

You almost have it. you just need to read your checkbox state and send it back with your POST request.

$(function(){
    $("#myonoffswitch").click(function(){
        var isOn = $("#myonoffswitch").prop("checked");
        $.post("notification.php", { 'onoffswitch': isOn }, function (result) {
            //handle success here
        });
    });

这篇关于如何使用jquery检查复选框时更新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆