如何在jQuery成功的情况下执行查询?

How can I execute a query on success of jQuery

本文关键字:情况下 执行 查询 成功 jQuery      更新时间:2023-09-26

我使用jQuery从数据库中删除一些数据。我想要一些功能,当jQuery返回成功时,我想执行一个查询。我想在没有页面刷新的情况下更新jQuery成功的另一个表。我可以这样做吗?如果可以,我该怎么做?

我是jQuery的新手,所以如果这不是一个很好的问题,请不要介意。

这是我的脚本:

<script type="text/javascript">
$(document).ready(function () {
    function delete_comment(autoid, btn_primary_ref) {
        $.ajax({
            url: 'rootbase.php?do=task_manager&element=delete_comment',
            type: "POST",
            dataType: 'html',
            data: {
                autoid: autoid
            },
            success: function (data) {
                // I want to execute the Update Query Here
                alert("Comment Deleted Successfully");
                $(btn_primary_ref).parent().parent().hide();
                var first_visible_comment = $(btn_primary_ref).parent().parent().parent().children().find('div:visible:first').eq(0).children('label').text();
                if (first_visible_comment == "") {} else {
                    $(btn_primary_ref).parent().parent().parent().parent().parent().parent().prev().children().text(first_visible_comment);
                }
                load_comment_function_submit_button(autoid, btn_primary_ref);
            },
        });
    }
    $(document).on('click', '.delete_user_comment', function (event) {
        var autoid = $(this).attr('id');
        var btn_primary_ref = $(this);
        var r = confirm("Are you sure to delete a comment");
        if (r == true) {
            delete_comment(autoid, btn_primary_ref);
        } else {
            return false;
        }
    });
});
</script>

你不能直接在Javascript中做数据库操作。您需要做的只是在成功时向后端的php文件发出一个新的AJAX请求,以更新给定的表。但是,这意味着对后端有两个AJAX请求,这两个请求都管理数据库数据。似乎有点多余。为什么不在php文件本身的删除操作之后进行更新操作呢?

添加一个服务器端编码页面来执行您的查询。例子:假设您添加了一个名为executequery.php的页面。使用下面的代码:

当您想要执行查询时,请执行以下操作:

$.post("executequery.php",//the URL of the page
{
param1:value1,
param2:value2....//if you want to pass some parameters to the page if not set it to null or {}
},
function(data){
//this is the callback that get executed after the page finished executing the code in it
//the "data" variable contain what the page returened
}
);

PS:发送给页面的参数被视为php页面中的$_POST变量


有一个其他的解决方案,但它的不安全我建议使用它。它可以发送带有参数的查询,这样你就可以用相同的页面执行任何查询:

$.post("executequery.php",//the URL of the page
{
query:"insert into table values("
param1:value1,
param2:value2....//if you want to pass some parameters to the page if not set it to null or {}
},
function(data){});