Results 1 to 9 of 9

Thread: Problem with PHP Strings in JavaScript

  1. #1
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with PHP Strings in JavaScript

    Hi there all,

    I have this following code which is on a timer, here it is:
    Code:
    <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:
    Code:
    <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

  2. #2
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Is it an actual .js file or is inline in the document?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  3. #3
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi there,

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

    Thanks very much.

    Best Regards,
    Private_Guy

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Ok, I just saw the issue, it should be <?=$TIME_LIMIT?>
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  5. #5
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    The difference was the dollar sign in front of the variable:

    Code:
    <?=$TIME_LIMIT?>
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  7. #7
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  8. #8
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Haha, ok, misunderstood, sorry. I'm glad you got it working.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  9. #9
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    No its okay, thanks very much for helping me

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •