It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

AJAX/PHP Countdown Problem When Page Refreshed...

AJAX/PHP Countdown Problem When Page Refreshed...

Postby bigbluesky2006 on Mon Sep 24, 2007 7:12 am

I have put together some code to countdown to a date specified in a PHP file using the server time to calculate the time difference.

The problem is that although the countdown is displayed correctly when the page is first loaded, when the page is refreshed in the browser the counter goes to zero.

Can anyone point out where I'm going wrong?

I'm using three files: index.html, ajaxcountdown.js & servertime.php.

index.html...

Code: Select all
<html>
<head>
<title></title>
<SCRIPT language=JavaScript src="functions.js"></SCRIPT>
</head>
<body>
<P>Countdown ends:</P>
<script src="ajaxcountdown.js"></script>
</body>
</html> 


ajaxcountdown.js...

Code: Select all
document.writeln('<span class="" style="color:#f00;font-size:39px;font-weight:bold;" id="countdowntimer"></span>');

InitiateTimer();

function UpdateTimer()
{
   TimeLeft--;
   strText = ''
   if (TimeLeft <= 0)
   {
      strText = "NOW!!!";
   }
   else
   {
      strText = parseInt(TimeLeft/86400)+ " Days "+parseInt(TimeLeft/3600%24)+ " Hours "+parseInt(TimeLeft/60%60)+ " Minutes "+parseInt(TimeLeft/1%60)+ " Seconds ";   
   }
   document.getElementById('countdowntimer').innerHTML = strText;
}
var TimeLeft = -1;
function InitiateTimer()
{
   var http;
   var http = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
   http = new XMLHttpRequest();
   if (http.overrideMimeType) {
   http.overrideMimeType('text/xml');
   }
   } else if (window.ActiveXObject) { // IE
   try {
   http = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
   try {
   http = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e) {}
   }
   }
   if (!http) {
   alert('Agent unable to establish communication  :( ');
   return false;
   }

   http.open("GET", "servertime.php", true);
   http.onreadystatechange = function()
   {      
      if (http.readyState == 4) {
          //var response = http.responseText;
          //a = response.split('\n');
          //TimeLeft = a[1];
          TimeLeft = http.responseText;
          setInterval("UpdateTimer()",1000);
      }   
   }
   http.send(null);
}


servertime.php...

Code: Select all
<?php
// The number of the month when the countdown ends (1-12)
$month=12;
//The number of the day when the countdown ends (1-31)
$day=5;
//The number of the year when the countdown ends (yyyy)
$year=2008;
// How do you want the countdown displayed?
// 1 = days
// 2 = hours
// 3 = minutes
// 4 = seconds
$display = 4;
// DON'T EDIT AFTER THIS LINE
// --------------------------
$target = mktime(0, 0, 0, $month, $day, $year);
$today = time ();
$difference =($target-$today);
if ($display == 1) { echo ($difference/86400); }
if ($display == 2) { echo ($difference/3600); }
if ($display == 3) { echo ($difference/60); }
if ($display == 4) { echo ($difference); }
?>


Any help will be much appreciated! :)
User avatar
bigbluesky2006
 
Posts: 10
Joined: Fri Dec 15, 2006 7:44 am

Postby bobbyblades on Mon Sep 24, 2007 10:10 am

why do people go through all that trouble with ajax? when you can just use a hidden iframe to message back to it's parent window? ajax is so sloppy because you have all that cross browser coding. my advice is ditch ajax like the plague and go to a hidden iframe.
User avatar
bobbyblades
50+ Club
 
Posts: 85
Joined: Fri Aug 24, 2007 8:38 pm
Location: hawaii or bust

Postby dafunkymunky on Mon Oct 01, 2007 6:26 pm

A hidden i-frame is a dirty way of doing it. Thou its easy. Ajax is much more than a hidden i-frame.

About the problem..

First paste this code directly on to the main html page itself

<span class="" style="color:#f00;font-size:39px;font-weight:bold;" id="countdowntimer"></span>

dont complicate things by using document.writeln

call InitiateTimer() function in the body tag..

put this piece of code in the head section.
<script src="ajaxcountdown.js"></script>

i'll test the code now and will get back to you soon
--------DAFUNKYMUNKY--------
::THE::NECESSARY::[D]EVIL::
....errr i mean munky....
User avatar
dafunkymunky
100+ Club
 
Posts: 183
Joined: Fri Apr 08, 2005 7:32 am
Location: India

Postby bobbyblades on Mon Oct 01, 2007 7:00 pm

LOL are you serious? how many lines of code for a hidden iframe versus ajax? ajax is sloppy because you need to code for 2 browser versions then make a request then react to the request via an event blah blah blah ajax is the most over hyped technology ever. maybe it will be useful one day when it's cross browser and less wordy. in other words when it's more programmer friendly.
User avatar
bobbyblades
50+ Club
 
Posts: 85
Joined: Fri Aug 24, 2007 8:38 pm
Location: hawaii or bust

Postby dafunkymunky on Mon Oct 01, 2007 7:06 pm

working fine bro..

just do the changes i asked you to.
--------DAFUNKYMUNKY--------
::THE::NECESSARY::[D]EVIL::
....errr i mean munky....
User avatar
dafunkymunky
100+ Club
 
Posts: 183
Joined: Fri Apr 08, 2005 7:32 am
Location: India


Who is online

Users browsing this forum: No registered users and 8 guests