dou760663 2018-11-04 08: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 09: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 
          } 
       }) 
    })   
    

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部