1

I'm trying to use PHP variables in Javascript but I couldn't. After over 2000 lines of writing different JS functions I was fine avoiding that but now I really needed it. I'm a bit lost on all the ways to go about this but nothing really worked. Here is my sequence:

index.html file:

...
<script src="myfunctions.js" />
....

myfunctions.js file:

....
function test() {
    var x = <?php echo $_conf['user_id'];?>
    console.log(x);
}

I was trying to rename the .js file into .php file and add

header("Content-type: text/javascript");

at the beginning - that didn't work. I was trying to make .htaccess file with

AddType application/x-httpd-php .js

But that didn't work either. I'm probably missing just a tiny thing. I just need someone fresh and bright to point it out.

1
  • echo is not Javascript. At the very least, try return instead. Commented Nov 12, 2013 at 20:52

6 Answers 6

2

You can do something like this within your JS code.

var php_var = "<?php echo $_conf['user_id'];?>"
Sign up to request clarification or add additional context in comments.

1 Comment

Get that extra echo out of there, and close the ?>
1

Your javascript file should be named as "javascript.php" (just put the name you want, the only important thing is the .php

You have an index.php

Write in your index.php

include("javascript.php");

Then in your javascript.php

<script>
function test(){
     var variable = "<? echo $conf['user_id'] ?>";
     alert(variable);
}
<script>

PS: Yo don't need any header.

3 Comments

This appears to be the only completely correct answer. Changing the file extension(s), and adding var something=
I agree that this one is the correct answer in fact this one only worked. But I have noticed one thing - all the functions in javascript.php file now became visible on the page, i.e. if I save that page on a disk I see all the functions while when it was .js file a user didn't have access to those. Would that be a problem for security?
The user can always access your javascript code, even in a .js file. It won't be saved in the file if they download it, but they will still know the URL and can download it separately.
1

Since you're doing this via a <script> tag, your PHP script MUST output valid Javascript code, as if you'd literally type your variable assignment in manually. That means doing something like:

HTML/JS:

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

PHP:

<?php
$myvar = 'foo';
?>

var myvar = <?php echo json_encode($myvar); ?>;

Which in the end, will produce somethign that will function exactly as if you'd manually typed in the following:

<script>
var myvar = 'foo';
</script>

Note the use of json_encode(). Using this ensures that whatever you're outputting from PHP will become syntactically valid Javascript.

2 Comments

Why json ? Isn't needed.... you can just use var myvar = '<? echo "whatever"; ?>'; I use it every time...
it's a safety measure. consider $name = "Miles O'Brien" and echoing that into a JS block. Boom. Syntax Error. Kiss your script block goodbye.
1

You're not assigning PHP value to a Javascript variable. Try:

var v = "<?php echo $_conf['user_id'];?>";

1 Comment

@NelsonGaldemanGraziano: Thanks, bad copy/paste from question. Corrected.
0

index.html

 ..
<script src="myfunctions.js.php" />
 ...

myfunctions.js.php

<?php
header('Content-Type: text/javascript');
 ...
?>
var val = <?php echo json_encode($val); ?>;
 ...

Comments

0

Other possible solution is to assign server-side data to attributes in html and read them in javascript. For example index.html could contain something like this:

<div id="user-profile" data-user-id="<?php echo $conf['user_id']; ?>"></div>

and in js file you can get them while necessary(example with jQuery):

var userID = $('#user-profile').attr('data-user-id');

Of course you should adjust your server-side settings to process html files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.