Treehouse: Grow your CSS skills. Land your dream job.

php and javascripts intermingling

  • # February 20, 2010 at 3:40 pm

    I have at the top of my index.php page the code that links to a .js file. Inside this .js file i need to have a variable which was defined inside my index page but i do not know how to accomplish this.

    Any help appreciated –

    # February 20, 2010 at 7:24 pm

    what about trying to make a display:none element thats contains this variable lets say

    < ?php
    $name = 'Your name';


    and in the javascript i assume that you use Jquery

    we will select the var div and get the value of it’s content

    var Svalue = $(‘#var’).text();

    if this do what you want or dont , please let me know

    # February 20, 2010 at 7:39 pm

    second thought i found my first replay was ridiculous

    ok lets say thats the header are created with php
    so simply you can make this

    < ?php
    $varibleName='Dynamic Value ';
    < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

    < ?php//the reset over here ?>

    the only trick here is to defined the variable before the calling the javascript file that you use the variable on it .

    i hop you like it.


    # February 21, 2010 at 11:06 am

    Hi, thanks for the reply – I have tried what you said but i’m not sure i did it correctly as it did not work.
    However your first idea worked perfectly, i created a new div with its contents as an echoed php variable, set it’s display to hidden and used document.getElementById in js file, Thanks A Lot!

    # February 24, 2010 at 9:55 pm

    your welcome and you can put your code over here for the second solution

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.