简体   繁体   English

在我的表{AJAX}中选中复选框后如何使数据库更新值

[英]How do I make my database update a value when a checkbox is checked in my table { AJAX }

I'm wanting to make my code so that when I click in the checkbox to enable it, it updates a row in my database to the value 1. My checkbox is inside of my table in php and is coded around AJAX. 我想要制作我的代码,以便当我单击复选框启用它时,它会将我的数据库中的一行更新为值1.我的复选框位于我的PHP表格内,并围绕AJAX编码。 My issue is that when I click the checkbox, 1) it doesn't do anything, (not even refresh the page using my ajax success status) : ` 我的问题是当我点击复选框时,1)它没有做任何事情,(甚至不使用我的ajax成功状态刷新页面):

success: function(data) {
   window.location.replace("admin_members.php");
}

2) It doesn't update my database information giving my column named 'enabled' a value of 1 for when it's checked from the PHP receiver. 2)当从PHP接收器检查时,它不会更新我的数据库信息,从而使名为'enabled'的列的值为1。

My Code is Below : 我的代码如下:

Html / PHP Table (checkbox is included in here) Html / PHP表(复选框包含在这里)

<td style="border: 1px solid #eee; text-align: center; font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'enabled','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["enabled"]  == 1 ? '<input id="enabled-checkbox" value="1" type="checkbox" checked >' : '<input id="enabled-checkbox" value="1" type="checkbox" >'; ?></td>

AJAX Sender AJAX发件人

        function showEdit(editableObj) {
            $(editableObj).css("background","#FFF");
        } 

        function saveToDatabase(editableObj,column,id) {
            $(editableObj).css("background","#FFF url('images/spin.gif') no-repeat right");
            $.ajax({
                url: "includes/saveedit_members.php",
                type: "POST",
                data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
                success: function(data){
                    window.location.replace("admin_members.php");
                }        
           });
        }

PHP Receiver PHP接收器

if(!isset($_POST['column']) || !isset($_POST["editval"]) || !isset($_POST["id"])) {
    header('Location: error-pages/index.php');
} else if(isset($_POST['column']) && isset($_POST["editval"]) && isset($_POST["id"])) {
    mysqli_query($con, "UPDATE users set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE  id=".$_POST["id"]);
}

A few picture examples to help understand my database and why I'm trying to do : 一些图片示例,以帮助了解我的数据库以及我为什么要这样做:

I want it so if the checkbox is checked, automatically refresh page after updating database column 'enabled' to 1 我想要它,如果选中该复选框,则在将数据库列'enabled'更新为1后自动刷新页面

我现在已经完成了:)经过努力工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM