When a user navigates away from a particular webpage, I need to update the database via PHP code, preferably without a popup window. The code below opens a "Confirm Navigation" popup, and works if I respond "Leave this Page", by navigating away and running the code on "mypage". However, if I respond "Stay on this Page" it will still run the code on "mypage", which I don't want to happen.
<script type="text/javascript">
window.onbeforeunload = CallPHP;
function CallPHP () {
var a = new XMLHttpRequest();
a.open("GET","https://mydomain.com/mypage",true);
a.send();
return "Thank you.";
}
</script>
I don't know how to call the PHP file directly, and I don't know how to avoid the popup and just let the user navigate away. Any help greatly appreciated.