Im working on an ecommerce website which has a booking process composed by multiple steps, and I need to run some php code when the user leaves from the booking process ( so closes the browser from any of the steps, or clicks back multiple times, until he leaves the step1 ), I tried using the js unload/beforeunload events, something like:
var executeAjax = function (evt) {
jQuery.ajax({
url: my_php_url,
async: false
});
window.beforeunload = executeAjax
$(window).unload(executeAjax)
But the problem is that this code gets executed when the user passes from step1->step2->step3 etc... and also when he refreshes the browser, or click back/forward, but I only need that code to be ran when he leaves the booking process ( so the next url he accesses is not step1,step2, it can be external URL's also, not only my website.... or he closes the browser) and I tried several possibilities and none worked.
Does anyone has an idea of how to solve this, I would be grateful. Thanks.