Jump to content

sharing php and javascript variables


saiko

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?
Link to comment
https://forums.phpfreaks.com/topic/9911-sharing-php-and-javascript-variables/
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: [email protected]
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]
[!--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?
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.

Archived

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

×
×
  • 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.