Jump to content

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.
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.
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.
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 ;)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.