单击甜蜜警报调用 php 文件的按钮后

after clicking a button of sweet alert call php file

我试图在单击甜蜜警报中的按钮后调用 php 文件。此功能是登录帐户的确认。示例过程是当您注销活动帐户时,会有一个弹出消息是甜蜜的警报,如果您单击确定按钮,那么它应该已经破坏了用户的会话。有人可以帮我弄这个吗?

这是我的代码:

<a href="javascript:swal({title:'Logout', 
text:'Do you want to logout this Account?'
    , icon:'warning', 
    buttons: true, 
    dangerMode: true}).then((willOUT) => {
    if (willOUT) {
          url: 'page_logout.php', {
          icon: 'success',
        });
      }
    });" 
    class="nav-item dropdown-item">             
    Log out
</a>

see the console

试试 js:

<button type="button" onclick="logout('<?php echo $SESSION['user']['userId'] ?> ')" class="btn-danger">logout</button>
function logout(id) {
    swal({
        title: "Do you want to logout this Account?",
        type: "error",
        showCancelButton: true,
        confirmButtonClass: "btn-danger",
        confirmButtonText: "Yes!",
        cancelButtonText: "No",
        closeOnConfirm: true,
        closeOnCancel: true
    },
            function (isConfirm) {
                if (isConfirm) {
                    userLogout(id);
                }
            });
}

function userLogout(id) {
    $.post(base_url + "fileName/method_name", {id: id}, function (data) {
        if (data === "1") {
            }
         else if (data === "0") {
            swal("", "Error to logout user.", "warning");
        } else {
            swal("", data[0], "error");
        }
    });
}

这对你有帮助。它可以带你去 page_logout.php 您在

中编写的 swal 代码中也存在一些错误
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="#" id="a_id">Logout</p>
    <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#a_id").click(function(){

                    swal({title:'Logout', 
                        text:'Do you want to logout this Account?', 
                        icon:'warning', 
                        buttons: true, 
                        dangerMode: true
                    })
                    .then((willOUT) => {
                            if (willOUT) {
                                  window.location.href = 'page_logout.php', {
                                  icon: 'success',
                                }
                              }
                    });

            });
        });
    </script>
</body>
</html>

或者如果你想在有注销按钮的同一页面中显示消息,那么在你的问题中不要 url 而是 ajax 请求 page_logout.php。希望对你有帮助。

您可以将注销事件与功能一起使用

<script src="https://unpkg.com/sweetalert2@7.8.2/dist/sweetalert2.all.js"></script>

<a onClick="logout()">Logout</a>
<script>
function logout(){
 swal({
  title: 'Logout',
  text: 'Do you want to logout this Account?',
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#DD6B55',
  confirmButtonText: 'Yes!',
  cancelButtonText: 'No.'
}).then(() => {
  if (result.value) {
    // Call ajax page_logout.php File
  } else {
    // Close alert
  }
});

}
OR 
functio logout() {
swal({
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!'
  }).then(function() {
    swal(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    )
  })
}
</script>