Since AJAX calls are asynchronous by default , before calling $.post page closes and the result is not sent to your server.
You need to use the synchronous ajax call here to make sure the page closes after sending the message.
$(window).unload(function() { $.ajax({ url: "record_action.php", async: false, type: 'POST', success: function(){
source share