View Full Version : php variables from common include are not available in js.php file unless duplicated
Beverleyh
01-14-2011, 02:00 PM
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;
$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> ?
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:
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.
fastsol1
01-15-2011, 01:07 AM
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?
short answer: yes.
long answer: yes, but it's easy to run into problems
Beverleyh
01-15-2011, 12:16 PM
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.
jscheuer1
01-15-2011, 12:51 PM
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
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/showthread.php?p=225674#post225674
and the script it's used with:
http://www.dynamicdrive.com/dynamicindex4/php-photoalbum.htm
Which also has a less compatible, but perhaps easier to follow version of the code:
http://www.dynamicdrive.com/dynamicindex4/phpgallery/getalbumpics.php.txt
thank you traqypoos.
lol, whut? :D
Beverleyh
01-16-2011, 12:23 AM
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. ;)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.