onUnload event not working

I have a test page where there is a 15 minue time limit.
I also have an unOnload event which should submit a hidden form field to record the time  emaining if the user navigates away from the page or refreshes.

The unOnload event does not seem to submit the form as the form always passes the value zero.

Can any one help.
I call ther unOnload event in the body tag.
Here is the relevent part of ther javascript and the form:

<title>English Test</title>

<script type="text/javascript">
var timeLeft = <?=$timeLeft ?>;
var tId = "";
//var leave = "";
function countdown() {
       timeLeft -= 1000;
      if (timeLeft<=0){
      } else {

window.onload=function() {
  tId = setInterval('countdown()',1000);
function updateTimer() {
   var timerArea = document.getElementById('timer');
   var remArea = document.getElementById('eng1time');
   var numSeconds = timeLeft / 1000;
   var displaySeconds = numSeconds % 60;
   var displayMinutes = (numSeconds - displaySeconds) / 60;

   // format display a little more by putting a leading 0 for small seconds
   if (displaySeconds < 10) {
       displaySeconds = "0" + displaySeconds;

      timerArea.value = displayMinutes + ":" + displaySeconds;
      remArea.value = timeLeft;
      function nextPage(){
            //assuming you have <form id="English1"...>
            var f = document.getElementById("English1");
            for( var i=0; i < f.elements.length; ++i)
                    if( String(f.elements[i].type).toLowerCase() != "submit" )

            alert('Time allowed for the English test has expired! Please press the Maths Test button to continue');

       function activate(f){
  if (timeLeft > 100000 && confirm('Are you sure you want to leave the English test? You still have some time left to go back and check your answers. If you are sure you want to go on to the Maths Test, please click OK button. Otherwise, click Cancel button to return to the English test and use the remaining minutes to improve your English score.'))
return false;
            for( var i=0; i < f.elements.length; ++i)
      return true;


<body text="#000000" style="background: #e5e5e5; text-align:center; height:2500px;" ONUNLOAD="window.document.getElementById('form1').submit()">
<form id="form1" method="GET" action="<? echo $_SERVER['PHP_SELF']; ?>">
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michel PlungjanIT ExpertCommented:
You cannot rely on the onUnload to have access to anything on the page

You can use onBeforeUnload on some browsers or continuously set cookies and onUnload load a page that looks at the cookies
ErikalAuthor Commented:
I susspected that onUnload event was not a very good idea.
So how would I be able to trigger the form to pass its single hidden field variable value to a php session variable when the user navigates away from the page? Is there any way other than using cookies?
Michel PlungjanIT ExpertCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ErikalAuthor Commented:
Thank you.
I research that link.
Hi Erikal, I have been following this question to, as I am interested in the solution. Before I suggested you try asking the question in a different zone, I tried using onBeforeUnload, but I am not good enough at javascript to figure how how to get the current time into the database in a javascript event, so can you show what worked for you?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.