I need to run a query when the page is closed by the user. Apparently; window.onbeforeunload us the correct event. In fact I can see that it triggers.
Now, the problem is that for some reason (unknown to me) when issuing an AJAX request like this AJAX does not run:
window.onbeforeunload = function(e) {
jQuery.ajax({
type: 'POST',
url: 'release_all_holds.php',
datatype: 'json',
data: { SEATS: JSON.stringify(selectedSeats),
},
async: false,
});
}
It also does not work when using navigator.sendBeacon like this:
window.onbeforeunload = function(e) {
var headers = {type: 'application/json'};
var blob = new Blob(JSON.stringify(selectedSeats), headers);
navigator.sendBeacon('release_all_holds.php', blob);
};
I know it did not run because I check the table in mysql and see it has not updated.
Has anyone here actually been successful doing this. Maybe provide code I can use.