Forum Moderators: coopster

Message Too Old, No Replies

passing php variable to javascript

         

jackvull

11:57 am on Oct 6, 2005 (gmt 0)

10+ Year Member



Hi
I am trying to pass a php variable to javascript but keep getting javascript errors. Below is the code:

echo " <script language = 'javascript' type='text/javascript'>
<!--

var mins = ".$timelimit.";
var secs = 60;

function changeClock() {
if (!(mins == 0 && secs == 0)) {
if (secs == 0) {
mins -= 1;
secs = 60;
}
secs -= 1;
document.getElementById('clock').innerHTML = mins + 'mins ' + secs + 'seconds left to next refresh';
}
}
setInterval('changeClock()', 1000);

function reloadwindow () {
window.location = 'RegularEmailScript.php?Send=yes&start=".$start."&origtotal=".$origtotal."';
}

setTimeout('reloadwindow()', ".$timeout.");
// -->
</script>

";

I have also tried just writing th javascript in and using var mins = <?php echo ($timelimit);?>; but this doesn't seem to work either.
Any ideas?

jackvull

12:05 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Sorry. Foget that.
The part echiong the javascript was inside a function and I hadn't decalred the variables as global.
!

coopster

1:37 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Variable scope [php.net] will get you every time. Nice catch, jackvull.