Okay I've got this script and it works everywhere else except when I call it from the OnUnload event handler. Can php be used with the OnUnload event handler??

Script is

<?php
function bob2()
{
$active_file = fopen ("active.txt", "r");
$active_old = fread ($active_file, filesize ("active.txt"));
$active_new = $active_old + 1;
fclose ("$active_file");
$file = fopen ("active.txt", "w");
fwrite ($file, $active_new);
fclose ("$file");
}
?>

<BODY OnUnload="<?php bob2(); ?>">
.......

.......

When I delete the above line of coding the webpage works as normal. Any tips?

    You are mixing up JavaScript and PHP. That function will actually run every time you write it:

    i.e. <BODY OnUnload="<?php bob2(); ?>">

    This will actually run the function as the page is loading. If you want it to run onunload you will actually have to put the code in a sepatate file and call it in the onunload

    i.e. <BODY OnUnload="document.location=bob2.php">

    I am not sure if the above works, go to a porn site an see how they manage to popup a new window when you close the dozen they have already opened. It is in the name of research.:p

      You can't run server-side scripts on the client, which is what your code is trying to do.

      Maybe if your onunload() handler did a Javascript document.location() call to a PHP script that ran bob2().

      However you do it, something has to be sent back to the server.

        okay then...i guess i better do sum reading then. i thought i might work though because the onClick event handler works (well i think so anyway)

          use javascript to open a small window onUnload, with url of the php file. and keep output of the php file as
          <html>
          <body>
          <script>
          self.close();
          </script></body></html>

          simple enough ........ eh 😉

            i think that last post was helpful....i think i shall just put a window.close at the end so i don't bother my viewers.

              Write a Reply...