I found that this works sporadically, but usually doesn’t work at all.
$(window).unload(function()
{
$.ajax
({
url: "script.php",
type: "POST",
data:
{
value: value
}
});
});
Setting the async to false (i.e.'async' : false) does get around the problem and executes the php script every time. But depending on the script, it can freeze the browser up for a while.
Is there a good way of sending data to a php script and having the server execute it without the user waiting for a response?
EDIT – My understanding of client-server interaction is rusty, but it’s not the request sending that’s taking a long time, it’s the processing of the data sent by the request. I’m wondering if that processing can occur without the user having to be on the page at all.
You would need to accomplish this on the server side by closing the connection and then continuing to process the data. In php it would look something like this:
?>
Note this isn’t technically firing a background task so you should be fine doing this on a shared host. Just as long as it doesn’t hang for an exceptionally long period of time.