【问题标题】:How to Execute query only when user click to cofirm on Leave option.otherwise query should not be execute仅当用户单击以确认离开选项时如何执行查询。否则不应执行查询
【发布时间】:2017-05-22 07:15:39
【问题描述】:

这是我的代码

function myfoo(){
    $.ajax({
            type: "POST",
            url: "update.php",
            dataType: 'json',
            data: {
              on_timeout: 1
            },
            success: function (r) {}
       });
    }

     window.onbeforeunload = function(){
          myfoo();
          return 'Are you sure you want to leave?';
     };

我的 update.php 代码:

session_start(); 
include 'conn.php';

if(isset($_SESSION['Seats'])) {
    $seatS=$_SESSION['Seats']; 
    $Eventid=$_SESSION['Eventid'];
    $cats = explode(" ", $seatS); 
    $cats = preg_split('/,/', $seatS, -1, PREG_SPLIT_NO_EMPTY); 
    foreach($cats as $key => $cat ){ 
        $cat = mysql_real_escape_string($cats[$key]); 
        $cat = trim($cat);
        if($cat !=NULL) { 
            $stmt = $con->prepare('UPDATE fistevent SET Status=" " where Event_Id=? AND seats="'.$cat.'" AND Status="Hold" '); 
            $stmt->bind_param("s", $_SESSION['Eventid']); 
            $stmt->execute(); 
        }
    }
}

【问题讨论】:

    标签: php ajax onbeforeunload


    【解决方案1】:

    试试下面beforeunload绑定:

    $(window).on('beforeunload', function () {
       return 'Are you sure you want to leave?';
    });
    $(window).on('unload', function () {
       console.log('calling ajax');
       myfoo();
    });
    

    【讨论】:

    • 不工作先生。现在它不显示任何警报消息。意味着离开或留下
    • 您收到确认框了吗?确定并取消选项?只有当你点击确定按钮时它才会执行功能
    • 我上面的代码是运行状态..它也显示离开或保持警惕。但是当我点击关闭浏览器时,它会直接执行查询。但是我想在点击离开按钮时执行查询
    • @krishna 据我了解,当用户单击浏览器的关闭选项卡时,必须执行警报框。我是对的
    • 先生,我知道上面的代码是完美的。但是请在您的本地主机上尝试我的代码。它有什么问题
    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 2019-01-01
    • 2012-01-26
    • 2013-03-25
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多