strobic Posted December 29, 2010 Share Posted December 29, 2010 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> Quote Link to comment Share on other sites More sharing options...
requinix Posted December 29, 2010 Share Posted December 29, 2010 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. Quote Link to comment Share on other sites More sharing options...
the182guy Posted December 29, 2010 Share Posted December 29, 2010 Please don't create a Goodbye! popup when somebody closes your site, people will start to hate you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.