Jump to content

Archived

This topic is now archived and is closed to further replies.

lukeprice

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

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

[quote]Also, the script needs to update the current page[/quote]
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.

Share this post


Link to post
Share on other sites
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:

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

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

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 ;)

Share this post


Link to post
Share on other sites
[quote author=micah1701 link=topic=100196.msg395174#msg395174 date=1152628516]
I echo Daniel's comments.
it doesn't HAVE to be AJAX
[/quote]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.