how to make countdown timer to not reset on page refresh


Problem :

I am creating an online exam page for my project. I have a countdown timer, but it resets on page refresh. How can I make it not to reset? The timer is set by fetching time from db. I am using php mysql. Please help me. Here is my code.

<?php
   $result=mysql_query("select * from test where testid='29'");
   while($time=mysql_fetch_array($result)){
      $dateFormat  = "d F Y -- g:i a";
      $targetDate  = time() + ($time['duration']*60);
      $actualDate  = time();
      $secondsDiff = $targetDate - $actualDate;
      $remainingDay      = floor($secondsDiff/60/60/24);
      $remainingHour     = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
      $remainingMinutes  = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
      $remainingSeconds  = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));
      $actualDateDisplay = date($dateFormat,$actualDate);
      $targetDateDisplay = date($dateFormat,$targetDate);
   }
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Untitled Document</title>
   <script type="text/javascript">
      var days = <?php echo $remainingDay; ?>  
      var hours = <?php echo $remainingHour; ?>  
      var minutes = <?php echo $remainingMinutes; ?>  
      var seconds = <?php echo $remainingSeconds; ?> 
      function setCountDown ()
      {
          seconds--;
          if (seconds < 0){
             minutes--;
             seconds = 59
          }
          if (minutes < 0){
              hours--;
              minutes = 59
          }
          if (hours < 0){
              hours = 23
          }
          document.getElementById("remain").innerHTML = "  "+hours+" hr "+minutes+" min    "+seconds+" sec";
          SD=window.setTimeout( "setCountDown()", 1000 );
          if (minutes == '00' && seconds == '00') { 
              seconds = "00"; window.clearTimeout(SD);
              window.location = "result.php"
          } 

       }
    </script>

</head>
<body onLoad="setCountDown();">
   <div id="remain">
         <?php echo "$remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?>
   </div>


Solution :

You should read the value into session variable and using that afterwards:

<?php
session_start();
if (isset($_SESSION['targetdate'])) {
    // session variable_exists, use that
    $targetDate = $_SESSION['targetdate'];
} else {
    // No session variable, red from mysql
    $result=mysql_query("select * from test where testid='29' LIMIT 1");
    $time=mysql_fetch_array($result);
    $dateFormat = "d F Y -- g:i a";
    $targetDate = time() + ($time['duration']*60);
    $_SESSION['targetdate'] = $targetDate;
}

$actualDate = time();
$secondsDiff = $targetDate - $actualDate;
$remainingDay     = floor($secondsDiff/60/60/24);
$remainingHour    = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-         ($remainingHour*60*60))/60);
$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-    ($remainingHour*60*60))-($remainingMinutes*60));
$actualDateDisplay = date($dateFormat,$actualDate);
$targetDateDisplay = date($dateFormat,$targetDate);

?>

Mysql Tutorials

Mysql Howto..

How to improve SQL query w/ JOINs?

How to get last 4 inserted ID's in MySQL

How can I write SQL for a table that shares the same name as a protected keyword in MySql? [duplicate]

MySQL error 1093. how to delete from this

How does one store 'packages' of multiple products within a database

How To Get The Difference of a Two Date Value in MySQL and the result is in month wise?

How to adjust display settings of mysql command line?

How to set up a specific registration on Database

how to find albums using mysql query having 2 assets for each of its tracks

How do I get python Parallel to work with local mysql server?

Show WARNINGS in MySQL + PDO

How to store openssl_public_encrypt() output in MySQL database?

How to append a select result columns to anther select result in mysql

How to remove a prefix name from every table name in a mysql database

How to optimize this Mysql query

How to store special characters from Twitter API into MySQL?

How should I join these tables MySQL?

AJAX / jQuery / PHP / MySQL - how to update a database

How to output multiple columns from one MySQL column?

Django: how to install mysql/connector python with pip3

How do I use MySQL Joins to select forum boards that users are allowed to read?

How do I conditionally sort on a second column using a custom MySQL function?

How to set group_concat_max_len with MySQL and Hibernate

How to get paragraph without editor points from mysql database? [closed]

How do I retrieve results as multidimensional array from mySQL and PHP?

How to rename a view in MySQL?

How to use the mysql “LIKE” syntax with a timestamp?

How to convert PHP array into Mysql resource

how to implement Northwind in Django

how to connect Website to MYSQL database?