user164863
user164863

Reputation: 641

Getting PHP variable in Javascript code

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.

Upvotes: 1

Views: 2793

Answers (6)

ConstantineUA
ConstantineUA

Reputation: 1051

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.

Upvotes: 0

Castiblanco
Castiblanco

Reputation: 1198

You can do something like this within your JS code.

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

Upvotes: 2

anubhava
anubhava

Reputation: 785286

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

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

Upvotes: 1

Marc B
Marc B

Reputation: 360732

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.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798794

index.html

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

myfunctions.js.php

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

Upvotes: 0

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.

Upvotes: 1

Related Questions