1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Inserting PHP Variable Into Javascript

Discussion in 'PHP' started by bloard, Apr 1, 2007.

  1. #1
    Don't know if this should go in the php forum or javascript, but I can hack around with php a little... know nothing about javascript.
    SEMrush
    But, can I insert a php variable into javascript on the same page? Meaning if I define a variable on a php page as $var=2... then later on the page have some javascript and within that javascript is a variable like "jsvar"... Can I just echo the php variable, $var, within the javascript code where jsvar is defined?

    In other words, I want jsvar = $var. It would seem like since php is server-side, that the php variable would be inserted into the js code before the js reaches the client... so, in my mind it should work. Does it?
     
    bloard, Apr 1, 2007 IP
    SEMrush
  2. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Yes, it should work. I include JS in my PHP scripts all the time :) You might just use PHP to print out the JS and include your PHP variable directly into the print statement. Or something like that :p
     
    Louis11, Apr 1, 2007 IP
  3. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <script language=javascript>
    var jsvar;

    jsvar = <?php echo $phpvar;?>
    </script>
     
    jitesh, Apr 1, 2007 IP
  4. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Enclose the php code in quotes if the variable is a string
    jsvar = "<?php echo $phpvar;?>"
    Code (markup):
     
    Aragorn, Apr 1, 2007 IP
  5. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Aragorn is right.
     
    jitesh, Apr 1, 2007 IP
  6. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I use a lot of PHP in my Javascript. course it works.
     
    manilodisan, Apr 2, 2007 IP
  7. crazydeeznuts

    crazydeeznuts Banned

    Messages:
    4
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I use this style: <?=$phpvar?> inside of my javascripts, it does the same thing, just shorter code, and every little bit helps! :cool:
     
    crazydeeznuts, Apr 6, 2007 IP
  8. Clive

    Clive Web Developer

    Messages:
    4,506
    Likes Received:
    296
    Best Answers:
    0
    Trophy Points:
    250
    #8
    Not recommended. Always use <?php echo $phpvar ?> for printing.
     
    Clive, Apr 6, 2007 IP