Issue with JavaScript interpreting a PHP variable

I have 2 PHP files: fileA.php and fileB.php

In fileA.php, I have a constant and some variables. One such variable (we’ll call it “$test” is assigned a function call for a Boolean value (it’s the one with the argument of ‘register’).

In fileB.php, I have some echo statements that echo JavaScript (so in essence, fileB.php is actually more of a JavaScript file in PHP clothing). In this file, I try to use $test in the following way:

echo 'var test = " ' . $test . ' " ; ' ;

In doing this, I receive a “function not defined” message through the JavaScript console of Firebug. So thinking that this has something to do with the inclusion of fileA.php into fileB.php, I include fileA.php elsewhere in another different file to echo the contents of $test only to discover that it works fine in that other file… So it’s making me believe that this error has something to do with the way the JavaScript is being handled through the PHP echos.

What are some ways I can fix this?

The contents of fileA.php is below:

<?php
	$siteurl = ($_SERVER["HTTP_HOST"] == 'localhost') ? 'http://localhost/websites/blah' : 'http://www.blah.com/user';
	define('ABSPATH2', $siteurl);
	include ABSPATH2 . '/includes/functions.php';
	$test = blahFunction('register');
	echo $test;
?>

“blahFunction()” is defined inside of the functions.php file…

The partial contents of fileB.php is below (the file is pretty large as it uses Adobe Spry):

<?php
	include '../fileA.php';
	echo 'var test = "'.$test.'";';
	echo 'var dir = "'.ABSPATH2.'/stuff/morestuff/directory/c/";
	window.onload = function(){setScreenClass();};
	window.onresize = function(){setScreenClass();};
...

The error message about the undefined function is also saying that there’s a missing semi-colon from fileA.php but I’ve scoured that page for hours and can’t find anything wrong with it–hence my coming to the pros on here.

Any help on this would be appreciated as always. :slight_smile:

The window.onLoad and window.onResize are javascript syntax and should be out of the php brackets (<? ?> )

Also I havent ever used PHP varibles with javascript, just javascript variables with javascript and html ids and classes so I am interested in learning how this plays out.

Regards,
Team 1504

there is no method window.onload in php that is javascript. If you wan’t to echojavascript into html do it like this


<script language="javascript" type="text/javascript">
<?php echo 'var top = "' + $var + '"';
echo 'window.onload = function() {...}';
?>
</script>

your way is irregular.

Javascript errors occur on the client side, so your first step should be to view the source of the page (i.e. the output from your PHP scripts - press Ctrl+U in Firefox) to look for errors there. Checking the PHP scripts might not make the error immediately clear, as would checking the Javascript that it outputs.

Post the content of the page that is received by the browser that corresponds to the snippet from FileB.php that you posted. It will be the part where the value of “test” is set in the Javascript code. I expect that the problem will be clear when you check this output.

Also, the snippet of FileA.php that you posted does not have a missing semicolon. I am intrigued, however, to learn how the Javascript error is referencing FileA.php when that file is included on the server-side (based on what you have posted).