Results 1 to 4 of 4

Thread: php within external javascript

  1. #1
    Join Date
    Jan 2010

    php within external javascript

    I have a php variable which uses some preset functions from a CMS to give me a file location on a server.

    What I need to be able to do is use this variable within my external javascript file. I'm ok with javascript, fine with php but combining the two is giving me a bit of a headache.

    I've read I can simply have my script.js file as script.php and have javascript then in it, but this doesn't ever seem to work for me and the js gets ignored?

    Any help would be great,

    L.U.N.E for anyone and everyone who has an interest in literature

  2. #2
    Join Date
    Mar 2012
    You can add the value of a php variable to js like this:

    echo "<script>var name_of_varible=$php_value;";</script>
    Than you can recall the rest of the code like you want, including file or adding the code directly.

  3. #3
    Join Date
    Jan 2007
    Columbia, MD
    I have tried to do exactly what this says and I cannot get it to work. Here is what I am trying to do and the code.The Game.php calls the first PHP file and gets the number from the dice, shows it on the screen but doesn't store it in the variable (which I have declared in Game.php as a global variable) for future use.
    Here is the PHP code:
    $response = mt_rand(1,6);
    echo $response;
    echo '<script>currentQuote = $currentQuote;</script>';//this not working
    echo "<br>";
    echo "currentQuote is:";
    echo $currentQuote; //this shows on the screen fine
    Shouldn't currentQuote be available for use in Javascript?

  4. #4
    Join Date
    May 2006
    Somewhere behind your screen
    $output.='<script>currentQuote = '.$currentQuote.';</script>';//this working
    $output.='<br />currentQuote is:';
    $output.=$currentQuote; //this shows on the screen fine
    echo $output;
    signature under construction

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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