timer opens and closes immediately by submitting questions without allowing students to attempt examination
<p>Please can anyone help out as the countdown timer on my e-exam just stop working and I couldn't figure out what actually happened... the timer will read zeros (hh:min:sec) and submit student questions automatically without allowing them to answer questions and thereby grade them(students) zero percent. thanks...</p>

<pre><code>function countDown() 
{
      sec--;
  if (sec == -01) {
    sec = 59;
min = min - 1;
if(min==-01)
    {
        min=59;
        hour=hour-1;
    }else
        {
            hour=hour;
    }
  } else {
   min = min;
  }
if (sec&lt;=9) { sec = "0" + sec; }



time = (hour&lt;=9 ? "0" + hour : hour) + " : " + (min&lt;=9 ? "0" + min : min) + " : " + sec + "";
if (document.getElementById) { document.getElementById('timer').innerHTML = time; }



SD=window.setTimeout("countDown();", 1000);
if (hour=='00'&amp;&amp; min == '00' &amp;&amp; sec == '00') { sec = "00";min="00"; window.clearTimeout(SD); window.location=document.forms[0].action+"?fs=yes";}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  countDown();
});
</code></pre>

<p>the php section of the script is this:</p>

<pre><code>&lt;?php



error_reporting(0);
session_start();
include_once 'oesdb.php';
$final=false;
if(!isset($_SESSION['stdname'])) {
    $_GLOBALS['message']="Session Timeout.Click here to &lt;a href=\"index.php\"&gt;Re-LogIn&lt;/a&gt;";
}
else if(isset($_REQUEST['logout']))
{
    //Log out and redirect login page
       unset($_SESSION['stdname']);
       header('Location: index.php');

}
else if(isset($_REQUEST['dashboard'])){
    //redirect to dashboard
    //
     header('Location: stdwelcome.php');

    }else if(isset($_REQUEST['next']) || isset($_REQUEST['summary']) || isset($_REQUEST['viewsummary']))
    {
        //next question
        $answer='unanswered';
        if(time()&lt;strtotime($_SESSION['endtime']))
        {
            if(isset($_REQUEST['markreview']))
            {
                $answer='review';
            }
            else if(isset($_REQUEST['answer']))
            {
                $answer='answered';
            }
            else
            {
                $answer='unanswered';
            }
            if(strcmp($answer,"unanswered")!=0)
            {
                if(strcmp($answer,"answered")==0)
                {
                    $query="update studentquestion set answered='answered',stdanswer='".htmlspecialchars($_REQUEST['answer'],ENT_QUOTES)."' where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";";
                }
                else
                {
                    $query="update studentquestion set answered='review',stdanswer='".htmlspecialchars($_REQUEST['answer'],ENT_QUOTES)."' where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";";
                }
                if(!executeQuery($query))
                {
                // to do
                $_GLOBALS['message']="Your previous answer is not updated.Please answer once again";
                }
                closedb();
            }
            if(isset($_REQUEST['viewsummary']))
            {
                 header('Location: summary.php');
            }
            if(isset($_REQUEST['summary']))
             {
                     //summary page
                     header('Location: summary.php');
             }
        }
        if((int)$_SESSION['qn']&lt;(int)$_SESSION['tqn'])
        {
        $_SESSION['qn']=$_SESSION['qn']+1;

        }
        if((int)$_SESSION['qn']==(int)$_SESSION['tqn'])
        {
           $final=true;
        }

    }
    else if(isset($_REQUEST['previous']))
    {
    // Perform the changes for current question
        $answer='unanswered';
        if(time()&lt;strtotime($_SESSION['endtime']))
        {
            if(isset($_REQUEST['markreview']))
            {
                $answer='review';
            }
            else if(isset($_REQUEST['answer']))
            {
                $answer='answered';
            }
            else
            {
                $answer='unanswered';
            }
            if(strcmp($answer,"unanswered")!=0)
            {
                if(strcmp($answer,"answered")==0)
                {
                    $query="update studentquestion set answered='answered',stdanswer='".htmlspecialchars($_REQUEST['answer'],ENT_QUOTES)."' where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";";
                }
                else
                {
                    $query="update studentquestion set answered='review',stdanswer='".htmlspecialchars($_REQUEST['answer'],ENT_QUOTES)."' where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";";
                }
                if(!executeQuery($query))
                {
                // to do
                $_GLOBALS['message']="Your previous answer is not updated.Please answer once again";
                }
                closedb();
            }
        }
        //previous question
        if((int)$_SESSION['qn']&gt;1)
        {
            $_SESSION['qn']=$_SESSION['qn']-1;
        }

    }
    else if(isset($_REQUEST['fs']))
    {
        //Final Submission
        header('Location: testack.php');
    }
?&gt;
&lt;?php
header("Cache-Control: no-cache, must-revalidate");
?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Examination Platform
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;
    &lt;meta http-equiv="CACHE-CONTROL" content="NO-CACHE"/&gt;
    &lt;meta http-equiv="PRAGMA" content="NO-CACHE"/&gt;
    &lt;meta name="ROBOTS" content="NONE"/&gt;
    &lt;link rel="stylesheet" type="text/css" href="oes.css"/&gt;
    &lt;script type="text/javascript" src="validate.js" &gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="cdtimer.js" &gt;&lt;/script&gt;
    &lt;script type="text/javascript" &gt;
    &lt;!--
        &lt;?php
                $elapsed=time()-strtotime($_SESSION['starttime']);
                if(((int)$elapsed/60)&lt;(int)$_SESSION['duration'])
                {
                    $result=executeQuery("select TIME_FORMAT(TIMEDIFF(endtime,CURRENT_TIMESTAMP),'%H') as hour,TIME_FORMAT(TIMEDIFF(endtime,CURRENT_TIMESTAMP),'%i') as min,TIME_FORMAT(TIMEDIFF(endtime,CURRENT_TIMESTAMP),'%s') as sec from studenttest where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid'].";");
                    if($rslt=mysql_fetch_array($result))
                    {
                     echo "var hour=".$rslt['hour'].";";
                     echo "var min=".$rslt['min'].";";
                     echo "var sec=".$rslt['sec'].";";
                    }
                    else
                    {
                        $_GLOBALS['message']="Try Again";
                    }
                    closedb();
                }
                else
                {
                    echo "var sec=01;var min=00;var hour=00;";
                }
        ?&gt;

    --&gt;
    &lt;/script&gt;

    &lt;/head&gt;
  &lt;body &gt;
      &lt;noscript&gt;&lt;h2&gt;For the proper Functionality, You must use Javascript enabled Browser&lt;/h2&gt;&lt;/noscript&gt;
       &lt;?php

        if($_GLOBALS['message']) {
            echo "&lt;div class=\"message\"&gt;".$_GLOBALS['message']."&lt;/div&gt;";
        }
        ?&gt;
      &lt;div id="container"&gt;
      &lt;div class="header"&gt;
                &lt;img style="margin:10px 2px 2px 10px;float:left;" height="80" width="200" src="images/logo.gif" alt=""/&gt;&lt;h3 class="headtext"&gt; &amp;nbsp;eExamination &lt;/h3&gt;&lt;h4 style="color:#ffffff;text-align:center;margin:0 0 5px 5px;"&gt;&lt;i&gt;Idea | Innovation | Motivation&lt;/i&gt;&lt;/h4&gt;
            &lt;/div&gt;
           &lt;form id="testconducter" action="testconducter.php" method="post"&gt;
          &lt;div class="menubar" style="text-align:center;"&gt;
              &lt;h2 style="font-family:helvetica,sans-serif;font-weight:bolder;font-size:120%;color:#f50000;padding-top:0.3em;letter-spacing:1px;"&gt;Examination Platform&lt;/h2&gt;
          &lt;/div&gt;
      &lt;div class="page"&gt;
          &lt;?php

          if(isset($_SESSION['stdname']))
          {
                $result=executeQuery("select stdanswer,answered from studentquestion where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
                $r1=mysql_fetch_array($result);
                $result=executeQuery("select * from question where testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
                $r=mysql_fetch_array($result);
          ?&gt;
          &lt;div class="tc"&gt;

               &lt;table border="0" width="100%" class="ntab"&gt;
                                    &lt;tr&gt;
                                        &lt;th style="width:40%;"&gt;&lt;h3&gt;&lt;span id="timer" class="timerclass"&gt;&lt;/span&gt;&lt;/h3&gt;&lt;/th&gt;
                                        &lt;th style="width:40%;"&gt;&lt;h4 style="color: #af0a36;"&gt;Question No: 1 &lt;/h4&gt;&lt;/th&gt;
                                        &lt;th style="width:20%;"&gt;&lt;h4 style="color: #af0a36;"&gt;&lt;input type="checkbox" name="markreview" value="mark"&gt; Mark for Review&lt;/input&gt;&lt;/h4&gt;&lt;/th&gt;
                                    &lt;/tr&gt;
                                &lt;/table&gt;
             &lt;textarea cols="100" rows="8" name="question" readonly style="width:96.8%;text-align:left;margin-left:2%;margin-top:2px;font-size:120%;font-weight:bold;margin-bottom:0;color:#0000ff;padding:2px 2px 2px 2px;"&gt;&lt;?php echo htmlspecialchars_decode($r['question'],ENT_QUOTES); ?&gt;&lt;/textarea&gt;
              &lt;table border="0" width="100%" class="ntab"&gt;
                  &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;&lt;td &gt;1. &lt;input type="radio" name="answer" value="optiona" &lt;?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&amp;&amp; strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optiona")==0 ){echo "checked";} ?&gt;&gt; &lt;?php echo htmlspecialchars_decode($r['optiona'],ENT_QUOTES); ?&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;&lt;td &gt;2. &lt;input type="radio" name="answer" value="optionb" &lt;?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&amp;&amp; strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optionb")==0 ){echo "checked";} ?&gt;&gt; &lt;?php echo htmlspecialchars_decode($r['optionb'],ENT_QUOTES); ?&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;&lt;td &gt;3. &lt;input type="radio" name="answer" value="optionc" &lt;?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&amp;&amp; strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optionc")==0 ){echo "checked";} ?&gt;&gt; &lt;?php echo htmlspecialchars_decode($r['optionc'],ENT_QUOTES); ?&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;&lt;td &gt;4. &lt;input type="radio" name="answer" value="optiond" &lt;?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&amp;&amp; strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optiond")==0 ){echo "checked";} ?&gt;&gt; &lt;?php echo htmlspecialchars_decode($r['optiond'],ENT_QUOTES); ?&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;th style="width:80%;"&gt;&lt;h4&gt;&lt;input type="submit" name="&lt;?php if($final==true){ echo "viewsummary" ;}else{ echo "next";} ?&gt;" value="&lt;?php if($final==true){ echo "View Summary" ;}else{ echo "Next";} ?&gt;" class="subbtn"/&gt;&lt;/h4&gt;&lt;/th&gt;
                      &lt;th style="width:12%;text-align:right;"&gt;&lt;h4&gt;&lt;input type="submit" name="previous" value="Previous" class="subbtn"/&gt;&lt;/h4&gt;&lt;/th&gt;
                      &lt;th style="width:8%;text-align:right;"&gt;&lt;h4&gt;&lt;input type="submit" name="summary" value="Summary" class="subbtn" /&gt;&lt;/h4&gt;&lt;/th&gt;
                  &lt;/tr&gt;

              &lt;/table&gt;


          &lt;/div&gt;
          &lt;?php
          closedb();
          }
          ?&gt;
      &lt;/div&gt;

           &lt;/form&gt;
     &lt;div id="footer"&gt;
          &lt;p style="font-size:70%;color:#ffffff;"&gt; &lt;/b&gt;&lt;br/&gt; &lt;/p&gt;&lt;p&gt;&lt;/p&gt;
      &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>