Jump to content

Archived

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

saiko

sharing php and javascript variables

Recommended Posts

riteo.

i have made a javascript countdown timer that resets after 5 minutes.

now there is a variable seconds for seconds and minutes for minutes which are originally and globally set.

the thing is that when u refresh the time goes back to the original time i want it to goto the countdown timers last time.

is there a way to do this in php ? or in javascript?

Share this post


Link to post
Share on other sites
i would also like to know how to update a database after that specific time.

so if the countdown is from 5 minutes to 0 minutes then when it hits zero it updates the database and the timer resets.

Share this post


Link to post
Share on other sites
You can't do this strictly with JavaScript or PHP. You have to combine them with AJAX or you need to reload the page with the location JS call.

Share this post


Link to post
Share on other sites
thanx its like you cleaned my spark plugs and my brain can start again :P

should of thought of this earlier. thanx.

ill go see if i can do it

edit: ive managed to do the updating db but how would i go about making the javascript variable update to the new seconds variable when refreshed?

would i update the db to the time or a txt file or update some javascript variables?

how could i get the javascript variable into php?

if you cant understand me and u have msn add me: saiko@final-designs.com

Share this post


Link to post
Share on other sites
You could try something like this

[code]<HTML>
<HEAD>
<script language="javascript">
    <?php
        $counter = isset($_GET['counter']) ? $_GET['counter'] : 300;
        echo "var counter = $counter";
    ?>

    function mycounter () {
        if (counter > 1)
            counter--;
        else
            counter = 300;
        
        // display counter    
        var obj = document.getElementById("counter");
        obj.innerHTML = counter;
        
        // store in hidden form field
        obj = document.getElementById("hidden");
        obj.value = counter;
        
        setTimeout("mycounter()", 1000);
    }
</script>
</HEAD>
<BODY onLoad = 'setTimeout("mycounter()", 0)'>

<SPAN id="counter" style="font-weight: 700; font-size: 24pt; color: red"></SPAN>

<FORM action="" method="GET">
        <input type="hidden" name="counter" id="hidden" value="0">
        <input type="submit" name="submit" value="Refresh page">
</FORM>
</BODY>
</HTML>[/code]

Share this post


Link to post
Share on other sites
thankyou im currently putting it into my script.

but one thing is how would i make it so it does the updating of the counter variable without having to submit the form ?

Share this post


Link to post
Share on other sites
[!--quoteo(post=374894:date=May 18 2006, 10:30 AM:name=saiko)--][div class=\'quotetop\']QUOTE(saiko @ May 18 2006, 10:30 AM) [snapback]374894[/snapback][/div][div class=\'quotemain\'][!--quotec--]
the thing is that when u refresh the time goes back to the original time i want it to goto the countdown timers last time.

is there a way to do this in php ? or in javascript?
[/quote]

How, and how often, are you refreshing the page if not by user action?

Share this post


Link to post
Share on other sites
yer i want it so when the user accesses the page it stores the variable to the hidden form etc.

then it updates the variable so then when the counter goes again it doesnt start from the original value.

or could i run the timer so it doenst have to do this.

this is for a game i am making.

my aim is for the timer to countdown to 0:00 then when it hits that it updates the databse and adds values like gold etc. then it resets to 5:00 and counts back down. i had that bit but when they accessed the page it went back to the original minute and second variable values.

so could i run this in another file or sumthin *confused*

note i have seen another site do this.

if u access it or refresh it, its timer will keep going or be like the rite time. so lets say u refresh and it takes to seconds then the timer will be eg. 14:14 then 14:12.

Share this post


Link to post
Share on other sites
*bump*

ok another question.

to update the database and add gold would i make a cron job or make a php script to do it.?

if its a cron job can i please have some guidence

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.