Jump to content

PHP script in jquery unload event


strobic

Recommended Posts

I'm trying to run a PHP script and want to have it execute when the user closes the browser. I have this code but it doesn't work. It runs when the page opens. Why doesn't it work?

 

<html>
<head>

<script type="text/javascript" src="jquery-1.4.4.min.js"></script>

</head>

<body>

<script type="text/javascript">

$(window).unload(function(){

alert("Goodbye!");

<?php
$fp = fopen("counter.txt", "r");
$count = fread($fp, 1024);
fclose($fp);
$count = $count + 1;
$fp = fopen("counter.txt", "w");
fwrite($fp, $count);
fclose($fp);
?>

});

</script>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/222925-php-script-in-jquery-unload-event/
Share on other sites

PHP and JavaScript are completely and utterly separate. You cannot have PHP do something on a JavaScript event, and you can't run JavaScript code inside your PHP.

 

The closest you can get is with AJAX: JavaScript pretends to be a browser and accesses a page on your site, which in turn triggers a PHP script.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.