如何将ajax放入确认按钮?

时间:2016-12-08 11:48:44

标签: javascript php jquery ajax function

朋友们,我正在尝试将ajax url放入确认按钮以更新数据库中的内容。

所以我在JavaScript部分

中这样做
function freeze_account() {
    $.confirm({
        title: 'Confirm!',
        content: 'This dialog will automatically trigger \'cancel\' in 6 seconds if you don\'t respond.',
        type: 'red',
        typeAnimated: true,
        boxWidth: '30%',
        useBootstrap: false,
        buttons: {
            confirm: function() {
                var manager_id = $('#manager_id').val();
                $.ajax({
                    url: "update_freeze.php",
                    type: "POST",
                    data: {
                        'manager_id': manager_id
                    },
                    success: function() {
                        location.reload();
                    }
                });
            },
            cancel: function() {}
        }
    });
}

这是更新代码

$manager_id = $_POST['manager_id'];
$state = '0';
$update=runQuery("UPDATE `users` SET `userStatus` =:userS WHERE `userID`=:user_id");
$update->bindparam(":userS",$state);
$update->bindparam(":user_id",$manager_id);
            $update->execute();

我的问题是,当我按下确认按钮ajax工作并转到另一页但数据库中没有任何事情发生。 我的代码有什么问题或者我想错过什么?

任何帮助任何想法我将不胜感激

最好的问候

2 个答案:

答案 0 :(得分:0)

看看我是如何解决我的问题的 也许我的代码有一个好处 感谢每一个建议或帮助我

function freeze_account() {


            var pid = $('#manager_id').val();


            bootbox.dialog({
              message: "Are you sure you want to Freeze this account ?",
              title: "<i class='glyphicon glyphicon-trash'></i> Freeze !",
              buttons: {
                success: {
                  label: "No",
                  className: "btn-success",
                  callback: function() {
                     $('.bootbox').modal('hide');
                  }
                },
                danger: {
                  label: "Freeze!",
                  className: "btn-danger",
                  callback: function() {




                      $.post('update_freeze.php', { 'pid':pid })
                      .done(function(response){
                          bootbox.alert(response);

                          location.reload();
                      })
                      .fail(function(){
                          bootbox.alert('Something Went Wrog ....');
                      })

                  }
                }
              }
            });


}

答案 1 :(得分:0)

您需要在按钮上设置事件侦听器。首先,确保您的按钮上有ID,以便我们抓住它。

现在,我们创建事件监听器:

$("#button").on("click", freeze_account());

而且,现在当您单击按钮时,ajax调用应该成功完成。

但是,由于其默认行为,它仍会重定向。

要覆盖此内容,只需阻止默认事件:

function freeze_account(event) {

    event.preventDefault(); // stops the button redirecting

    $.confirm({
        title: 'Confirm!',
        content: 'This dialog will automatically trigger \'cancel\' in 6 seconds if you don\'t respond.',
        type: 'red',
        typeAnimated: true,
        boxWidth: '30%',
        useBootstrap: false,
        buttons: {
            confirm: function() {
                var manager_id = $('#manager_id').val();
                $.ajax({
                    url: "update_freeze.php",
                    type: "POST",
                    data: {
                        'manager_id': manager_id
                    },
                    success: function() {
                        location.reload();
                    }
                });
            },
            cancel: function() {}
        }
    });
}
相关问题