Grow your CSS skills. Land your dream job.

php and javascripts intermingling

  • # February 20, 2010 at 3:40 pm

    Hi,
    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 -
    Thanks

    # February 20, 2010 at 7:24 pm

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

    Code:
    < ?php
    $name = 'Your name';
    print"

    “;
    ?>

    and in the javascript i assume that you use Jquery

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

    Code:
    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

    Code:
    < ?php
    $varibleName='Dynamic Value ';
    ?>
    < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




    < ?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.

    regards.

    # 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
    regards.

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

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".