Onclick背景颜色更改并保存在数据库中

时间:2018-09-02 01:19:35

标签: javascript jquery html mysql codeigniter

我有一行包含帐单字段。如果账单已付,则管理员将按“已付”按钮。结果将是更改该行的整个背景色,并且该颜色将保存在db中。现在的问题是我应该如何将该行的背景色保存在数据库中。

这是更改背景颜色但不保存在数据库中的代码。

function mark_complete(i){
         if(confirm('Are you sure you want to proceed with this action?')){
            $('tr').click(function () {
                if(this.style.background == "" || this.style.background =="white") {
                    $(this).css('background', 'yellow');
                }
                else {
                    $(this).css('background', 'white');
                }
            });
         }
}

1 个答案:

答案 0 :(得分:0)

假设您使用PHP作为服务器语言。 首先,我们将创建一个ajax函数来调用PHP脚本。 您将创建一个文件,用于在数据库中插入值。我将其命名为addColor.php

function mark_complete(i){
         if(confirm('Are you sure you want to proceed with this action?')){
            $('tr').click(function () {
                if(this.style.background == "" || this.style.background =="white") {
                    $(this).css('background', 'yellow');
                    $.ajax({     
                        type: "POST",
                        url: 'addColor.php',
                        data: {color:'yellow'},
                        success: function (data) {
                            console.log(data);   
                        },
                    });
                }
                else {
                    $(this).css('background', 'white');
                    $.ajax({     
                        type: "POST",
                        url: 'addColor.php',
                        data: {color:'white'},
                        success: function (data) {
                            console.log(data);   
                        },
                    });
                }
            });
         }
}

addColor.php

if(isset($_POST['color'])){
    $color = $_POST['color'];
    $con = new mysqli($db_hostname, $db_username, $db_password, $db_databasename);
    $stmt = $con->prepare("UPDATE `color_tbl` SET `color` = ?");
    $stmt->bind_param("s", $color);
    $stmt->execute();
    $stmt->close();
}

希望获得帮助,并给您一些想法