php - 如何在注销表单中使用 Sweet alert 2?

标签 php jquery sweetalert2

我有这个代码可以注销。这适用于标准确认框,但不适用于 Sweet alert 2。任何人都可以告诉我为什么吗?谢谢你。第一个代码是名为 dropzone.php 的页面中的表单,甜蜜警报代码也在此文件中。然后我有一个名为 logout.php 的单独文件。

    <form name="logoutform" method="post" action="logout.php" id="logoutform">
    <input type="hidden" name="form_name" value="logoutform">
    <button class="logoutform_button" type="submit" name="logout" value="Logga ut" id="logout"/></button>
</form>


//This works:
<script>
$(function(){
    $('#logout').click(function(){
        if(confirm('Are you sure to logout')) {
        return true;
        }

        return false;
        });
    });
</script>

//This do not work:
<script>
    $("#logout").on("click", function() {
        swal({
        title: 'Logga ut?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'OK'
    })  
    })  
</script>


//This is the code i have in the logout.php file:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['form_name'] == 'logoutform')
{
if (session_id() == "")
{
    session_start();
}

unset($_SESSION['password']);
header('Location: dropzone.php');
exit;
}  
?>

最佳答案

我想这会继续提交注销而不弹出 sweetalert。 要获得您想要的结果,请将登录按钮类型从“提交”更改为“按钮”。 传递回调以检查单击的按钮,如果确认单击。继续提交表格

<button class="logoutform_button" type="button" name="logout" value="Logga ut" id="logout"/></button>
<script>
$("#logout").on("click", function() {
    swal({
    title: 'Logga ut?',
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'OK',
    closeOnConfirm: true,
    closeOnCancel: true
   }).then((result) => { 
      if (result.value===true) { 
         $('#logoutform').submit() // this submits the form 
      } 
   }) 
})   

关于php - 如何在注销表单中使用 Sweet alert 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53142996/

相关文章:

javascript - 为可拖动对象添加颜色

javascript - 甜蜜警报 2 和 javascript :How do i make a Sweet alert button that sends me to a certain webpage when clicked

sweetalert2 类型错误 : this is undefined after v8 upgrade

php - 对两个表进行模糊匹配的选择查询

php - Sr.No 在 PHP MYSQL 中无法正常运行

javascript - 使用 jquery 验证多次上传的文件大小

jquery - 对于 >1000px 的值,向左动画图像不起作用

php - MYSQL:根据来自另一个表的 SELECT 更新行

php - 在 MySQL 中存储多个 GPS 坐标

angular - 单击确认按钮关闭 swal