Beforeunload 函数不起作用并且卸载后不更新记录
Beforeunload Function is Not working And not updating the records after unload
这是我的代码
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代码
<?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();
}
}
}
?>
AJAX data:
参数放在$_POST
,不是$_SESSION
.
如果您使用 mysqli
,则不能使用 mysql_real_escape_string
。使用 bind_param()
时不需要转义参数。您可以使用 explode()
而不是 preg_split()
,因为您的定界符中没有正则表达式模式; array_filter()
可用于删除空白条目。
<?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();
}
}
?>
这是我的代码
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代码
<?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();
}
}
}
?>
AJAX data:
参数放在$_POST
,不是$_SESSION
.
如果您使用 mysqli
,则不能使用 mysql_real_escape_string
。使用 bind_param()
时不需要转义参数。您可以使用 explode()
而不是 preg_split()
,因为您的定界符中没有正则表达式模式; array_filter()
可用于删除空白条目。
<?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();
}
}
?>