0

Say I have someJavascript.js file and somePHP.phpfile. In my php file, I have some $foo. How do I set a var something = $foo if it's in a different file?

Things I tried:

  1. var something = '<?php echo $foo ?>'; - This just gave me a string of exactly that phrase.
  2. var something = '<?php echo json_encode($foo); ?>'; Same thing, just a string.

Any help would be greatly appreciated! Thanks.

Evan Conrad
  • 3,993
  • 4
  • 28
  • 46
  • 1
    You can't use PHP inside a JS file if the file isn't processed by PHP. – Felix Kling May 11 '14 at 21:49
  • Things you tried and failed, because you attempted to process php script in `.js` file. – Rahil Wazir May 11 '14 at 21:50
  • There's no reason a separated JS file to be tight coupled to a PHP file – Royal Bg May 11 '14 at 21:58
  • @tereško I already looked there, didn't find my answer. :( – Evan Conrad May 11 '14 at 22:27
  • Then you should try to find a different hobby. It doesn't looks as if you have a talent in area, which requires independent research and willingness to experiment till-you-succeed. – tereško May 11 '14 at 22:28
  • @tereško You should find an ability to communicate with people. If simply asking a question after researching for hours is not what you call "independent research and willingness to experiment till-you-succeed", than you need to redefine your sense of what help sites are. You should respect the core concepts of this site; being friendly and civil is not telling people to give up. – Evan Conrad May 11 '14 at 22:34

4 Answers4

1

Your problem is that your webserver is not processing the .js file like it processes a .php file. All the .js files are to the browser as is.

A simple option would be to include a hidden element on somePHP.php then reference it in your someJavascript.js:

in somePHP.php:

<html>
    <body>
        <input type="hidden" id="foo" value="<?php echo $foo; ?>">
    </body>
</html>

in someJavascript.js:

var foo = document.getElementById('foo').value();
Jason
  • 15,017
  • 23
  • 85
  • 116
0

The correct way of doing this is using eval: var something = eval('') Keep in mind that json should be used carefully and there are a lot of json library that can parse json data into JavaScript objects in a safer way!

tmnd91
  • 449
  • 1
  • 6
  • 23
0

Kind of bulky to include the entire file, but it gets the job done, yes? Your file must be a .php file to use a php variable. (or your server has to be configured to process .js files with the php engine)

<?php include('somePHPfile.php') ?>


<script type="text/javascript" language="javascript">

var foo = "<?php echo $foo; ?>";

</script>
khaverim
  • 3,386
  • 5
  • 36
  • 46
0

The way I see it is you have three options:

  1. Jason's answer
  2. Setup your web server to parse .js files like php files. In Apache you can add AddType application/x-httpd-php .js to your apache2.conf or httpd.conf file. Similar idea in IIS. This idea only works if you can access your config files, so not on shared servers.
  3. Put your javascript into some file that is processed by the php parser. In a file with the .php extension you can always write a var a = '<?php echo $someVar; ?>';. Then in your original file write <script src='/path/to/your/phpScript.php'></script>. Essentially khaverim's answer.
BenFuller
  • 16
  • 1