更新查询 onbeforeunload 和
Update the query onbeforeunload and
这是我设置 $_POST
时的第一个查询:
<?php
if (isset($_POST['bags']))
{
$bagS=$_POST['bags'];
$id=$_POST["id"];
$_SESSION['id']=$id;
$cats = explode(" ", $bagS);
$cats = preg_split('/,/', $bagS, -1, PREG_SPLIT_NO_EMPTY);
foreach($cats as $key => $cat )
{
$cat = mysqli_real_escape_string($con,$cats[$key]);
$cat = trim($cat);
if($cat !=NULL)
{
$stmt = $con->prepare('UPDATE wallet SET `Status`="Hold" where `Id`=? AND `bags`="'.$cat.'" ');
$stmt->bind_param("s", $_POST["id"]);
$stmt->execute();
}
}
}
之后设置脚本超时以在用户端没有任何反应的情况下更新下一个查询。
<script type="text/javascript">
setTimeout(function() { window.location.href = "index.php?on_timeout=1"; }, 60 * 500);
</script>
然后脚本return超时时查询字符串并执行更新查询并重定向到其他页面:
if(isset($_GET['on_timeout'])) {
echo $bagS=$_SESSION['bags'];
echo $id=$_SESSION['id'];
$cats = explode(" ", $bagS);
$cats = preg_split('/,/', $bagS, -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 wallet SET `Status`=" " where `Id`=? AND `bags`="'.$cat.'" AND `Status`="Hold" ');
$stmt->bind_param("s", $_SESSION['id']);
$stmt->execute();
echo "<script type='text/javascript'>location.href = 'cards.php';</script>";
}
}
}
?>
现在我想在 window.onbeforeunload
的帮助下在用户关闭浏览器或返回浏览器时执行相同的查询,所以请告诉我如何在此事件中设置更新代码。
<script>
var areYouReallySure = false;
function areYouSure() {
if(allowPrompt){
if (!areYouReallySure && true) {
areYouReallySure = true;
var confMessage = "are you sure to do this";
return confMessage;
}
}else{
allowPrompt = true;
}
}
var allowPrompt = true;
window.onbeforeunload = areYouSure;
</script>
您可以在离开页面前调用您的代码
function myfoo(){
// Write your logic here
$.ajax({
url: "Pageurl_that_having_your_update_code",
dataType: 'json',
data: {
on_timeout: 1
},
success: function (r) {}
});
}
onbeforeunload:
window.onbeforeunload = function(){
myfoo();
return 'Are you sure you want to leave?';
};
或Jquery
$(window).bind('beforeunload', function(){
myfoo();
return 'Are you sure you want to leave?';
});
这是我设置 $_POST
时的第一个查询:
<?php
if (isset($_POST['bags']))
{
$bagS=$_POST['bags'];
$id=$_POST["id"];
$_SESSION['id']=$id;
$cats = explode(" ", $bagS);
$cats = preg_split('/,/', $bagS, -1, PREG_SPLIT_NO_EMPTY);
foreach($cats as $key => $cat )
{
$cat = mysqli_real_escape_string($con,$cats[$key]);
$cat = trim($cat);
if($cat !=NULL)
{
$stmt = $con->prepare('UPDATE wallet SET `Status`="Hold" where `Id`=? AND `bags`="'.$cat.'" ');
$stmt->bind_param("s", $_POST["id"]);
$stmt->execute();
}
}
}
之后设置脚本超时以在用户端没有任何反应的情况下更新下一个查询。
<script type="text/javascript">
setTimeout(function() { window.location.href = "index.php?on_timeout=1"; }, 60 * 500);
</script>
然后脚本return超时时查询字符串并执行更新查询并重定向到其他页面:
if(isset($_GET['on_timeout'])) {
echo $bagS=$_SESSION['bags'];
echo $id=$_SESSION['id'];
$cats = explode(" ", $bagS);
$cats = preg_split('/,/', $bagS, -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 wallet SET `Status`=" " where `Id`=? AND `bags`="'.$cat.'" AND `Status`="Hold" ');
$stmt->bind_param("s", $_SESSION['id']);
$stmt->execute();
echo "<script type='text/javascript'>location.href = 'cards.php';</script>";
}
}
}
?>
现在我想在 window.onbeforeunload
的帮助下在用户关闭浏览器或返回浏览器时执行相同的查询,所以请告诉我如何在此事件中设置更新代码。
<script>
var areYouReallySure = false;
function areYouSure() {
if(allowPrompt){
if (!areYouReallySure && true) {
areYouReallySure = true;
var confMessage = "are you sure to do this";
return confMessage;
}
}else{
allowPrompt = true;
}
}
var allowPrompt = true;
window.onbeforeunload = areYouSure;
</script>
您可以在离开页面前调用您的代码
function myfoo(){
// Write your logic here
$.ajax({
url: "Pageurl_that_having_your_update_code",
dataType: 'json',
data: {
on_timeout: 1
},
success: function (r) {}
});
}
onbeforeunload:
window.onbeforeunload = function(){
myfoo();
return 'Are you sure you want to leave?';
};
或Jquery
$(window).bind('beforeunload', function(){
myfoo();
return 'Are you sure you want to leave?';
});