PDA

View Full Version : Problem with PHP Strings in JavaScript



Private_Guy
07-02-2007, 01:21 AM
Hi there all,

I have this following code which is on a timer, here it is:

<script type="text/javascript">
for(var i=0;i<20;i++)
setTimeout("foo('"+i+"')",(20-i)*2000);
function foo(n){
document.getElementById('btn').value=n;
if(n==0){
document.getElementById('btn').value='Download Now!';
document.getElementById('btn').disabled=false;
}
} </script>

Which I have tried to replace the "20 seconds with a php string called "<?=TIME_LIMIT?> which equals to 20, this is what I did:

<script type="text/javascript">
for(var i=0;i<<?=TIME_LIMIT?>;i++)
setTimeout("foo('"+i+"')",(<?=TIME_LIMIT?>-i)*<?=TIME_LIMIT?>00);
function foo(n){
document.getElementById('btn').value=n;
if(n==0){
document.getElementById('btn').value='Download Now!';
document.getElementById('btn').disabled=false;
}
} </script>
But it didn't work :(. Howcome? Is it because you cant use PHP Strings in JavaScripts? Thanks very much for your help and support :)

Best Regards,
Private_Guy

alexjewell
07-02-2007, 02:18 AM
Is it an actual .js file or is inline in the document?

Private_Guy
07-02-2007, 02:20 AM
Hi there,

Thank you very much for the quick reply.
Online PHP File, exactly how the file is show above :D

Thanks very much.

Best Regards,
Private_Guy

alexjewell
07-02-2007, 02:28 AM
Ok, I just saw the issue, it should be <?=$TIME_LIMIT?>

Private_Guy
07-02-2007, 02:47 AM
Hi there,

Sorry but I used that as an example, of course u need <?=SITE_NAME?> but I just wrote that as simple but if you look at the code above, you will see that <?= and ?> and included. Thanks

Best Regards,
Private_Guy

alexjewell
07-02-2007, 02:59 AM
The difference was the dollar sign in front of the variable:



<?=$TIME_LIMIT?>

Private_Guy
07-02-2007, 03:08 AM
Hi there again,

No, I fixed it, you dont need the $ sign because on my config I have used it this way define("TIME_LIMIT","50"); not $time_limit= 50, thats why, lol. Thanks anways

Best Regards,
Private_Guy

alexjewell
07-02-2007, 03:15 AM
Haha, ok, misunderstood, sorry. I'm glad you got it working.

Private_Guy
07-02-2007, 03:18 AM
No its okay, thanks very much for helping me :)