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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.