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 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题