passing variable to javascript

Discussion in 'PHP' started by 3.5supersonic, Jun 23, 2010.

  1. #1
    Hi i need a help to pass php variable into a javascript, here is my javascript

    <script>
    $(document).ready(function() {
    $("#loader").load("content.php?uname=xxx");
    var refreshId = setInterval(function() {
    $("#loader").load("content.php?uname=xxx");
    }, 5000);
    });
    </script>

    i want to change xxx with php variable like

    <script>
    $(document).ready(function() {
    $("#loader").load("content.php?uname=$name");
    var refreshId = setInterval(function() {
    $("#loader").load("content.php?uname=$name");
    }, 5000);
    });
    </script>

    anyway to do like above?

    Thanks
     
    3.5supersonic, Jun 23, 2010 IP
  2. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    So why you can't write smth like this:
    
    <?php
    $name = 'whatever';
    
    print <<<HERE
    <script>
    $(document).ready(function() {
    $("#loader").load("content.php?uname=$name");
    var refreshId = setInterval(function() {
    $("#loader").load("content.php?uname=$name");
    }, 5000);
    });
    </script>
    HERE;
    ?>
    
    PHP:
     
    Kaimi, Jun 23, 2010 IP
  3. 3.5supersonic

    3.5supersonic Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks a lot Kaimi :)
     
    3.5supersonic, Jun 23, 2010 IP
  4. Scripts man

    Scripts man Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    its easy you can add the id in any html tage such as <a id='1'>
     
    Scripts man, Jun 23, 2010 IP
  5. 3.5supersonic

    3.5supersonic Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks all for the help.
     
    3.5supersonic, Jun 24, 2010 IP
  6. MayurGondaliya

    MayurGondaliya Well-Known Member

    Messages:
    1,233
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    170
    #6
    I am glad that you are happy with the solution you have got. There is one easier way.

    <script>
    $(document).ready(function() {
    $("#loader").load("content.php?uname=<?=$name?>");
    var refreshId = setInterval(function() {
    $("#loader").load("content.php?uname=<?=$name?>");
    }, 5000);
    });
    </script>
    
    PHP:
     
    MayurGondaliya, Jun 25, 2010 IP