user671891
user671891

Reputation: 165

Why is javascript not recognizing php code?

*edit: this particular situation was created while I was trying to debug another problem, here's a more thorough description of my problem: Why is javascript not able to use a javascript variable I declared in a php file?

Here's the issue:

I've got the following php code within a javascript file but it won't compile. The variable simple is used at the bottom when it's being passed to the function placem:

 <?php $simpleString = "i hope this works"; ?>


 var simple = "<?php echo $simpleString; ?>";

 window['mod0_2']= -.0015138;
 window['mod1_2']= -.3424094;
 window['mod2_2']= .40461099;

 window['lla0_2']= 43.6124872;
 window['lla1_2']= -116.20349;
 window['lla2_2']= 821.802867;

 window['publica2'] = new modd('milktruck','publica2',modScaler,'shadowrect3',1);

 placem('p2','pp2', simple, window['lla0_2'],window['lla1_2'],window['lla2_2']);

Upvotes: 0

Views: 2130

Answers (3)

George Marian
George Marian

Reputation: 2669

The typical scenario is to have HTML, JavaScript and PHP (or some other server side programming language/technology) hosted on a server. You would then access the website using a web browser from another machine. Again, this the typical scenario. All these components can exist on the same machine.

In the opensource world, the most popular web server is called Apache. There is a module for Apache (called mod_php) which interprets PHP code, executes it and returns its output. (Modules do exist for other programming languages, but I won't get into that.)

There are two basic approaches to what you'd like to accomplish, both have their pros and cons.

1) Configure mod_php to interpret *.js files as if they were PHP files.

2) Create your JavaScript files as PHP files. This is the same way that HTML is served from PHP files. The PHP code can be mixed in with HTML in the same file. The tag <?php ... ?> specifies the PHP code that should be interpreted by mod_php. Everything else is just sent to the browser as output.

Here is a very simple example of a very simply HTML page, with PHP used to provide the content of the <title> tag:

<html>
<head>
<title>
<?php echo 'Hello world'; ?>
</title>
</head>
<body>
</body>
</html>

That would appear in a PHP file on the server. In the browser, the resulting HTML source would look like the following. (That is, if you did a "view source" on the page. The page itself would appear blank and the title bar would contain the words: Hello world.)

<html>
<head>
<title>
Hello world
</title>
</head>
<body>
</body>
</html>

This can also be done with JavaScript instead of HTML. The important thing to keep in mind is that there are two views of this file. One, is what you see when looking at the raw file. The other is how that file looks after the PHP is executed and output is returned to the browser. What you are basically doing is combining output from PHP with HTML/JavaScript to form the final output that is sent to the browser for display.

Upvotes: 2

Lawrence Cherone
Lawrence Cherone

Reputation: 46650

milktruck.php

<?php 
 $simpleString = "i hope this works"; 

echo 'var simple = "'.$simpleString.'";

 window[\'mod0_2\']= -.0015138;
 window[\'mod1_2\']= -.3424094;
 window[\'mod2_2\']= .40461099;

 window[\'lla0_2\']= 43.6124872;
 window[\'lla1_2\']= -116.20349;
 window[\'lla2_2\']= 821.802867;

 window[\'publica2\'] = new modd(\'milktruck\',\'publica2\',modScaler,\'shadowrect3\',1);

 placem(\'p2\',\'pp2\', simple, window[\'lla0_2\'],window[\'lla1_2\'],window[\'lla2_2\']);';

?>


<script src="milktruck.php"></script>

you could make use of htaccess and keep the .js extension by having some modrewrites

Upvotes: 1

Burak Erdem
Burak Erdem

Reputation: 20009

Are you using PHP in a .js file? If so, Javascript runs on client side where PHP runs on server. If you want to execute PHP in a Javascript file, you have to rename that file to .php and include that new PHP file into your HTML.

Upvotes: 7

Related Questions