Results 1 to 9 of 9

Thread: php variables from common include are not available in js.php file unless duplicated

  1. #1
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default php variables from common include are not available in js.php file unless duplicated

    I'm having a bit of a problem with php variables in an external javascript file.

    Firstly, I have a common.php file included right at the top of all my pages and this php file contains variables that are used across the whole site - example;
    Code:
    $sitename = 'My Website';
    $author = 'Joe Bloggs';
    In the <head> section of the web pages I have an external javascript file converted to php;
    <?php header('Content-type: text/javascript');?>
    And linked like this;
    <script type="text/javascript" src="js/js.php"></script>

    Now the problem is that I can only seem to echo the common.php variables into the javascript if I duplicate the variables or include the common.php file again at the top of the actual js.php file.

    What I would like to do is use the variables from the common.php file originally included at the very top of the web page. Can this be done?

    The common variables are referenced successfully everywhere else in the web page body but its just via the external js.php file where I'm having problems.

    Am I missing something? (probably "yes" since I'm still learning php )

    Have I got to resort to putting the javascript internally in the <head> of the page, which works but I'd prefer the put the code in an external file?

    What are the implications of me just include()ing the js.php file rather than using <script type="text/javascript" src="js/js.php"></script> ?
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    if you're calling js.php via a script tag in the html, then it's being downloaded separately - and so, all the php files you included when you were building your page are not available to the js.php file.

    a few options, each more complex than the last:

    as you mentioned, simply include the js file:
    PHP Code:
    echo '<script>';
    include(
    'js.php');
    echo 
    '</script>'
    there's nothing terrible about doing this, but it can be messy sometimes, especially if it's a big script... I try not to.

    the next option is to include your "common.php" file at the beginning of the js.php script. You would need to make sure there's nothing that would accidentally get echoed or overwritten, or whatever, and end up causing errors in your javascript.

    a third option (which can really get out of hand, if you let it) is to pass those variables via a query string in the <script> tag (like so: <script src="js.php?var1=value&var2=othervalue&etc"></script> ). this will expose your variables, which might not be desirable. I use a similar technique to dynamically roll all of my javascript files into a single script, thus getting all the scripts I need (and none I don't) in a single request.

  3. The Following User Says Thank You to traq For This Useful Post:

    Beverleyh (01-15-2011)

  4. #3
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Sorry to not offer a bit of help, but I have been wondering for a while how to do php in a external js file. Is it as easy as putting header('Content-type: text/javascript'); at the top of the js file and save it as a php file?

  5. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    short answer: yes.
    long answer: yes, but it's easy to run into problems

  6. #5
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    if you're calling js.php via a script tag in the html, then it's being downloaded separately - and so, all the php files you included when you were building your page are not available to the js.php file.
    mmm, yes, I see - of course, that makes perfect sense - thank you traqypoos.

    An include() is what I was thinking while I was typing - certainly the easiest option in this case.

    I looked into doing it with query strings but they did look icky.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  7. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Not apropos to this discussion as a solution, rather as an example for one aspect of it (a fairly complex external javascript file written in PHP), here's an external javascript that's called with a query as to what the name of the array you want produced is:

    PHP Code:
    <?php 
    Header
    ("content-type: application/x-javascript"); 
    if (
    phpversion() >= 5.1){ 
       @
    date_default_timezone_set(date_default_timezone_get()); 


    function 
    returnimages($dirname=".") { 
       
    $pattern='/\.(jpg|jpeg|png|gif|bmp)$/i'
       
    $curimage=0
       if(
    $handle opendir($dirname)) { 
           while(
    false !== ($file readdir($handle))){ 
                   if(
    preg_match($pattern$file)){ 
             
    $filedate=date ("M d, Y H:i:s"filemtime($file)); 
                     echo 
    "        [$curimage, \"$file\", \"$filedate\"],\n"
                     
    $curimage++; 
                   } 
           } 
           echo 
    '        ["placeholder"]' "\n"
           
    closedir($handle); 
       } 


    $photovar=$_GET['id']; 
    if (!
    preg_match('/^[a-z_][a-z0-9_]+$/i'$photovar)){ 
        echo 
    'alert("Photo Album ID must contain only letters, numbers, or underscore, and cannot start with a number")'
        die(); 

    echo 
    "var $photovar={\n"
    echo 
    "    baseurl: \"http://" $_SERVER["SERVER_NAME"] . dirname($_SERVER['PHP_SELF']) . "/\",\n"
    echo 
    "    images: [\n"
    returnimages(); 
    echo 
    "    ],\n"
    echo 
    "    desc: []\n"
    echo 
    "}\n"
    die(); 
    ?>
    For more info on how this file is used, see:

    http://www.dynamicdrive.com/forums/s...674#post225674

    and the script it's used with:

    http://www.dynamicdrive.com/dynamici...photoalbum.htm

    Which also has a less compatible, but perhaps easier to follow version of the code:

    http://www.dynamicdrive.com/dynamici...umpics.php.txt
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by Beverleyh View Post
    thank you traqypoos.
    lol, whut?

  9. #8
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Well, there's just so much negativity on the web that I thought some light-hearted pet name calling would be fun - ha, ha

    Dont you feel brighter for it?

    I give pet names to nice, helpful people; suffixes of "poos" and "kins" tend to work best and dont normally result in a lashing like "sh1t" does.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  10. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

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
  •