This is my First Query when $_POST
is set:
<?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();
}
}
}
After that is set the script timeout to update next query if nothing happen from user side.
<script type="text/javascript">
setTimeout(function() { window.location.href = "index.php?on_timeout=1"; }, 60 * 500);
</script>
Then script return query string when timeout and execute update query and redirect to other page:
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>";
}
}
}
?>
Now I want to execute the same query on when user close the browser or back the browser with the help of window.onbeforeunload
, so please tell me how set the update code in this event.
<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>
You can call you code before leaving the page
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?';
};
Or Jquery
$(window).bind('beforeunload', function(){
myfoo();
return 'Are you sure you want to leave?';
});