Jump to content


Photo

JavaScript countdown timer reaches zero then some PHP script auto executes...


  • Please log in to reply
5 replies to this topic

#1 lukeprice

lukeprice
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 11 July 2006 - 02:18 PM

Hi,

Im totally new to PHP. Im pretty sure this can be done (someone suggested sleep() function but the timer needs to be real time and visible so I think it will have to be a javascript timer) but I don't know what the best way of doing it would be. I can get the JS timer in a form field the integer in that field to do certain things with PHP when you press submit but I want it to be automatic.

Countdown goes from 20 --> 0 and on 0 PHP script executes (reloads page or whatever).

Any help appreciated.

Thanks in advance.

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 11 July 2006 - 02:21 PM

Now that Web 2.0/AJAX has been so popular, you could use AJAX to do it. Or you could just use the Javascript function 'settimer' (I think it's called that) to set a timer and then go to another page where the PHP script will be run.

#3 lukeprice

lukeprice
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 11 July 2006 - 02:30 PM

I dont know anything about AJAX but if thats the only way of doing I suppose i'll have to learn :), ty

Also, the script needs to update the current page

#4 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 11 July 2006 - 02:35 PM

I echo Daniel's comments.
it doesn't HAVE to be AJAX (which allows you to run server side code after the client side script has loaded) - you can have the count down run and then go to a new page to run the PHP script.

but the important thing to remember is that PHP runs Server Side - before the page data is sent to the user's browser - and JavaScript is run Client Side (after the page has finished loading).  So it is impossible to run a PHP script after the page has loaded.

AJAX is a hack work around, but its not really running the PHP script from the current page, its running a different page, server side, and then forwarding those results to the current browser.

Also, the script needs to update the current page

then i would just forget AJAX  and have a javascript that counts down. when it gets to zero, pass whatever variables you have and reload the page, this time running the PHP script first.
"Confidence in the face of risk."

#5 lukeprice

lukeprice
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 11 July 2006 - 02:38 PM

Ok, thankyou. I think I need to do a few more tutorials before I attempt this then. Can anyone suggest any tutorials that are either focused on scripting for online gaming or would be useful in this area in the long run. My idea is to setup a simple text based browser game to learn a bit more about PHP, MySQL and have a bit of fun at the same time.

I was thinking of something along the lines of this script, probably makes no sense though... lol:

<?php
  $a = $_REQUEST['d2'] ;

 while ($a < 0.1)
  header ("Location: http://localhost/offline/index.html") ;
  
  
?>

d2 is a form field containing a JavaScript counter so a can grab the integer from there and check it using the REQUEST and if its below 0.1 I refresh the page, but it only works when you press submit :(.

NOTE: An example of the timer setup im looking for can be seen on the MMORPG Syrnia, if anyone has an account there, if you dont its pretty good ;)

#6 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 11 July 2006 - 02:43 PM

I echo Daniel's comments.
it doesn't HAVE to be AJAX


Never said he had to :P I said he could use a timer instead.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users