I want to include a PHP variable in an external javascript file. The PHP variable is used to set the javscript variable so the PHP variable is not being used. How would I do this? I've been trying this. Javascript code: javascriptvar = <?PHP echo($PHPvar); ?>; alert(javascriptvar); Code (markup): This code used to work, but now because it is in an external .js file it doesn't seem to work. Is this because the file type has to be .php. I thought that because I am including the .js file in the .php file it should just parse normally? Thanks. ~imozeb
myjavascript.php <?php header("Content-Type: text/javascript"); echo "var javascriptvar = ' ".$phpvar." '; "; echo "alert(javascriptvar);"; ?> PHP:
Then use this: <?php header("Content-Type: text/javascript"); ?> var javascriptvar = '<?php echo $phpvar; ?>'; alert(javascriptvar); PHP: