SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation php in javascript

    I am just a beginner in javascript, so here is my problem:

    I have a javascript file in which I set the paths to numerous items, such as .css file. I want that path to change depending on a value stored in a mysql table. The majority of my online application is php based, with only a few javascript files. If someone could post an example of how to do a mysql_query in a javascript file and then store the returned php value to a javascript variable, that would be awesome.

    Thanks in advance for your help!!!

  2. #2
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi there,

    Unfortunately you cannot use PHP vars inside JavaScript code, unless you actually generate the JS with PHP using echo. Or if you use a Smarty templating system, you could assign your PHP arrays/vars to your '.tpl' file which contains the JS.

    So you will still do all your MySQL queries in your PHP file, and then move from there.

    My opinion. I might be wrong...

    Antonie
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  3. #3
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hope this can help, it a php trick I use to do stuff like your request

    Code:
    <?php
    $var = "Passing php vars to javascripts is easy.";
    
    if ($_GET['js'] == 1) {
    
    	echo "function atest() {";
    	echo 'document.write("'.$var.'")';
    	echo "}";
    
    } else {
    	echo '<script src="/test.var.php?js=1" type="text/javascript"></script>';
    	echo '<script type="text/javascript">';
    	echo 'atest()';
    	echo '</script>';
    }
    ?>
    edit: euh kinda like the guy before me said ...

  4. #4
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help. I have another question though:

    How do I reference the .php file from the javscript file? Is there something like the include() function in php?

  5. #5
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by FunkyRunky
    Thanks for the help. I have another question though:

    How do I reference the .php file from the javscript file? Is there something like the include() function in php?
    Meaning? You want to include your PHP file into your JS file?
    If that's the case...you cannot do that.

    You need to generate your JS with your PHP code. Example :

    PHP Code:
    <?php

    echo
    "
    <script type=\"text/javascript\">
    function whatever()
    {
         blah blah blah
    }
    </script>
    "
    ;

    ?>
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,812
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    For an external Javascript that contains PHP

    Code:
    <script type="text/javascript" src="myjs.php"></script>
    The .php extension will have it process the PHP first and the script link will have the page load the result as Javascript.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    And for that you should put this at the top of myjs.php:
    PHP Code:
    header('Content-type: text/javascript'); 

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,812
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Raffles
    And for that you should put this at the top of myjs.php:
    PHP Code:
    header('Content-type: text/javascript'); 
    That isn't a Javascript command so it needs to be properly identified as PHP

    PHP Code:
    <?php header('Content-type: text/javascript'); ?>
    In practice most browsers dont need this since they have defined the content type in the script tag anyway.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh yes, I assumed he would have had the <?php and ?> tags there anyway.


Bookmarks

Posting Permissions

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