How do I pass a PHP variable into a window.onload event function? I have a URL that I need to pass to the JavaScript file that has this function in it. The JavaScript file itself is being linked from the PHP file that will pass the variable and I’ve seen many examples of how this is done via embedded JavaScript, but none where someone is linking to an external JavaScript file. I suppose this is probably a trivial matter to most of you, but I’ve never done this before and could use some guidance!
The PHP generates the HTML/JavaScript that the browser sees. It is just a matter of making sure that the PHP actually gets run first and the easiest way to do that is to use a ,php extension on the filename.
So for external JavaScript files generated from PHP you would use:
<script type=“text/javascript” src=“filename.php”>
Cool. I’ll give that a shot.
Okay Stephen, I did what you advised and I think I’m on the right track but I could use some expert advice / guidance…
The PHP file calling or including() in this scenario is called “index.php”. It’s including a PHP file called “j.php” (the file that has all the necessary JavaScript). Inside of j.php, I have the following code:
<script type="text/javascript">
var dir = '<?php get_css_url('css_url');?>';
window.onload = function(){set_class();};window.onresize = set_class;
function set_class(){
...
The processing is breaking with an error about an undefined function (the “get_css_url()” function). I’m not sure why this is, but I know it has something to do with the way I’m trying to initialize that variable “dir”–I just can’t seem to pin-point how I’m screwing it up. The function is references perfectly fine before the inclusion within the calling PHP file–just bombs-out when used within the JavaScript.
Any input on this is very appreciated.
Hi Wolf,
I strongly suggest that you do yourself a favor and try to incorporate the syntax that fengall suggested.
<script type=“text/javascript” src=“filename.php”>
The example code that you provided is a little confusing. The PHP code should be generating the javascript, not the other way around.
best of luck,
floater
floater, I thought I was! Ha.
The JavaScript that I’m trying to use here is in a PHP file. Honestly, the only reason I even needed to do any of this was to generate a URL variable that has the CSS directory in it as it’s value and this is produced by a PHP function–which is why I thought that generating the value the way I did above would be appropriate. Obviously, I’m dropping the ball somewhere, but if I am, how can I generate a JavaScript variable using a PHP function? This issue is what originally led me to inquiring about passing a PHP variable to a JavaScript function…
Maybe this will help (see below)?
<script type="text/javascript">
var dir = [B]VALUE GENERATED BY A PHP FUNCTION[/B]
window.onload = function(){blah();};
function blah(){
var w = 'data';
...do stuff...
}
</script>
Question: How would I use a PHP function in the above example to give “dir” a value? The value has to be generated from a PHP function in this… Now I hope you understand my emphasis about the PHP function that creates the value that gets stored in the JavaScript variable–the variable’s value is generated from a function that generates a directory where the CSS resides.
In order to be able to call a function in a script, the file that defines the function must be included in the script. You are calling the function get_css_url() in a script that generates JavaScript on the fly. Now include a file that defines the function get_css_url() in that script.
So basically what you’re saying Kailash is that any PHP functions that work outside of the JavaScript <script> element will NOT work within the same <script> element due to being defined prior to whatever is within the <script> tags?
The “get_css_url()” function is included before this “j.php” file include and since PHP is processed before JavaScript, why should I have to re-include the file that has the function defined in it? Is it because of the <script> tags?
Thanks for the input everyone. I feel like I’m definitely learning something here.
In what order are the scripts called in your page? If the script with the call to get_css_url() is called befor the script that actually contains it, regardless whether it’s PHP, you may get an error. I think that’s what Kailash was getting at.
Also, if your js script is actually being written through PHP, it’s fairly easy to plug any PHP variable you wish into it:
// j.php - JavaScript file - Written by Wolf 22 //
header("content-type: text/javascript");
...
window.onload = function() {
...
var myPHP_var = <?php echo $myPHP_var ?>;
var myPHP_string = "<?php echo $myPHP_string ?>";
...
}
Hope this helps.
Still having problems… I hope you’ll be a little more patient with me and check out a test case I have below because it’s synthesizing my issue with the other stuff previously mentioned (to some degrees, at least):
I have 3 files: index.php, variables.php, and j1.php (all in the same directory).
index.php code -
<?php include 'variables.php';?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Practice - Inclusion withhin JavaScript</title>
<style type="text/css">
.border{border:1px solid red;margin-top:1em;margin-right:auto;margin-left:auto;padding:0.5em;width:80%;}
</style>
</head>
<body>
<div class="border">
<p>This page simply tests inclusions within JavaScript.</p>
<div>
<?php //echo [B]$test[/B];?>[B]<!-- <-- Works if un-commented... -->[/B]
</div>
<div>
[B]<script type="text/javascript" src="http://localhost/practice/include/j1.php"></script>[/B]
</div>
</div>
</body>
</html>
variables.php code -
<?php [B]$test[/B] = '<p>Hello. The TEST variable is working.</p>';?>
j1.php code -
<?php include 'variables.php';?>
<?php echo [B]$test[/B];?>;
[B]alert("Hello! Looks like the JavaScript ALERT function is working...")[/B];
Oddly enough, the alert() function within j1.php would never work correctly unless I placed that extra semi-colon after the line that echo’s the $test variable–I have no clue why this is and it’s making me go nuts. Also, the $test variable doesn’t even echo at all when I try to echo it within the j1.php file but would echo if called from index.php… What gives? Isn’t an included file supposed to allow access to anything proceeding thereafter the inclusion??? Is my understanding of included files completely off here??? This really has me going bananas and I’m betting that I’m just having a stupid moment despite trying to do everything that you guys have advised.
The j1.php file is being interpreted as javascript code, the php code in j1.php is not interpreted as php coded.
Instead of using the javascript src attribute, you could instead include the j1.php file inside the script tag itself.
The resultant JavaScript looks like this:
<p>Hello. The TEST variable is working.</p>;
alert("Hello! Looks like the JavaScript ALERT function is working...");
The first line of it is completely invalid as JavaScript and if it were not for the ; added to the end of the line that garbage would be considered to continue onto the next line as well.
That the file extension is .php should be enough to force the code to be processed as PHP first and then the type=“text/javascript” gets the result of that treated as JavaScript. The problem is that the PHP code inside the code that is supposed to be generating JavaScript is generating HTML instead of JavaScript.
Stephen, I thought I understood what you are saying, but the following just doesn’t work:
index.php
<?php include 'variables.php';?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Practice - Inclusion withhin JavaScript</title>
<style type="text/css">
.border{border:1px solid red;margin-top:1em;margin-right:auto;margin-left:auto;padding:0.5em;width:80%;}
</style>
</head>
<body>
<div class="border">
<p>This page simply tests inclusions within JavaScript.</p>
<div>
<?php //echo $php_variable;?>
</div>
<div>
<script type="text/javascript"><?php include 'http://localhost/practice/include/j1.php';?></script>
</div>
</div>
</body>
</html>
j1.php
<?php echo $php_variable;?>
variables.php
<?php $php_variable = 'alert("Hello. The TEST variable is working.");';?>
When I try to echo the “$php_variable” inside of the “j1.php” file, it doesn’t work and since the JavaScript Console is reporting nothing, I think I’ll go crawl underneath a rock… I thought it may have something to do with the use of single quote marks around the value of the variable, but after trying the double quote marks, I realized that it’s something else. Any ideas?
That’s because with the code you have now $php_variable isn’t set to anything so when you echo it you get nothing.
Also you now have the alert code inside a string and are assigning it to the variable that you forgot to name.
Try your original variables.php code with the following j1.php:
<?php include 'variables.php';?>
document.write('<?php echo $test;?>');
alert("Hello! Looks like the JavaScript ALERT function is working...");
That provides some JavaScript to output the paragraph of content stored in the $test PHP variable. document.write() is the JavaScript equivalent of the PHP echo command to actually write the content into the web page.
Stephen, your last reply and a post you made over on another forum found here helped me pass the variable. FINALLY!
One last thing…
Any variable that I initialize in “j1.php” works flawlessly. If I initialize a PHP variable in “variables.php”, however, and include this file at the top of “index.php” where upon I then try to use this same variable in “j1.php”, it seems to elicit a “NULL” variable or one that isn’t even defined. Is my understanding of inclusions off? I’ll provide my latest code below for each file so that it may better illustrate what I’m referring to in this:
index.php -
<?php include 'variables.php';?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Practice - Inclusion withhin JavaScript</title>
<style type="text/css">
.border{border:1px solid red;margin-top:1em;margin-right:auto;margin-left:auto;padding:0.5em;width:80%;}
</style>
</head>
<body>
<div class="border">
<div>
<script type="text/javascript" src="j1.php"></script>
</div>
</div>
</body>
</html>
variables.php -
<?php $test = 'BLAH!!!';?>
j1.php -
<?php
$test = 'FINALLY, THE DAMN VARIABLE IS PASSING! THANKS STEPHEN!';
echo 'var $string = "'.$test.'";';
echo 'alert($string)';
?>
As you can see, the $test variable in j1.php will work fine, but if I comment that out and try to use the $test variable found in variables.php instead, it doesn’t work. From what I understood, I shouldn’t have to re-include variables.php inside of j1.php, should I??? Thanks again for your patience in all this–all of you have been a BIG help!
Yes you would need to include it again as j1.php is an entirely separate file to be sent from the server to the browser that will be accessed from the script tag in the web page. It is not a part of the HTML that is passed from the server and so the included files in the HTML are not accessible from the JavaScript because the two files are built separately on the server.