dou760663 2018-11-04 16:40 采纳率: 100%
浏览 57
已采纳

如何使用退出表格的Sweet alert 2?

i have this codes to logout. And this works with standard confirmation box, but not with Sweet alert 2. Can any one tell me wy? Thank you. The first code is the form in a page named dropzone.php the sweet alert code is also in this file. Then i have a separat file named 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;
}  
?>
  • 写回答

1条回答 默认 最新

  • doushaizhen1244 2018-11-04 17:10
    关注

    I suppose this goes ahead to submit the logout for without popping the sweetalert. To get you desired result, change the login button type to "button" from "submit". Pass a callback to check the clicked button, if confirm were click. go ahead to submit the form

    <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 
          } 
       }) 
    })   
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等