duanhuang1699 2017-08-31 05:51
浏览 386
已采纳

Beforeunload功能不起作用并且在卸载后不更新记录

Here is my code

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

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

update.php code

<?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();
            session_destroy();
        }
    }
}

?>
  • 写回答

1条回答 默认 最新

  • dongqiao0953 2017-08-31 06:31
    关注

    The AJAX data: parameters are put in $_POST, not $_SESSION.

    You can't use mysql_real_escape_string if you're using mysqli. You don't need to escape parameters when you're using bind_param(). And you can use explode() instead of preg_split(), since there's no regular expression pattern in your delimiter; array_filter() can be used to remove blank entries.

    <?php
    include 'conn.php';
    
    if(isset($_POST['Seats'])) {
        $seatS=$_POST['Seats'];
        $_SESSION['Seats'] = $seatS;
        $Eventid=$_POST['Eventid'];
        $_SESSION['Eventid'] = $Eventid;
        $cats = array_filter(array_map('trim', explode(',', $seatS)));
        $stmt = $con->prepare('UPDATE fistevent SET `Status`=" " where `Event_Id`=? AND `seats`= ? AND `Status`="Hold" ') or die($con->error);
        $stmt->bind_param("ss", $_POST['Eventid'], $cat);
        foreach($cats as $key => $cat ) {
            $stmt->execute();
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择