Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem with PHP and Javascript variable communication. I have this code:

<?php
$php_var = 'lol';
?>

<html>
<script type="text/javascript" charset="utf-8">
var php_var = "<?php echo $php_var; ?>";

alert(php_var);

</script>
</html>

This code does not work (as intended) for some reason. I cannot get the PHP variable's value passed down to Javascript variable. For some reason, Javascript completely ignores the php tags and assigns php_var a value of "". So it alerts the literal php code I put it as.

What am I doing wrong? I have been stuck on this problem for 3 hours. Is it my server's problem? (Using web hosting, dedicated). Thank you

share|improve this question
2  
Just so we're certain, you named the file with a .php extension correct? – JaredMcAteer Jul 24 '12 at 19:04
this is a .html file – Kalon Jul 24 '12 at 19:05
What can I do to solve this problem? Use AJAX? – Kalon Jul 24 '12 at 19:06
2  
I copy+pasted the code in your example as-is and it works perfectly fine. I received an alert with lol in it in both FireFox and IE. – newfurniturey Jul 24 '12 at 19:06
1  
@KalonCheong — Servers are usually configured to run .php files through the PHP engine but not .html files. – Quentin Jul 24 '12 at 19:13
show 2 more comments

closed as not a real question by Quentin, Richard Harrison, Druid, j0k, Kemal Fadillah Aug 7 '12 at 16:53

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

4 Answers

Use json_encode() to ensure you get a valid JavaScript expression (otherwise characters such as newlines and quotes will break things):

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

You also need to ensure PHP is actually enabled for the file. This is usually achieved by giving the file a .php extension.

share|improve this answer
What if you don't need JSON? – nico Jul 24 '12 at 19:06
2  
He needs a JavaScript string. json_encode will give him a valid JavaScript string when fed a string. – ThiefMaster Jul 24 '12 at 19:06
Ironically, it won't give valid JSON when fed a string :) – Quentin Jul 24 '12 at 19:16
But a valid JavaScript string/expression – ThiefMaster Jul 24 '12 at 19:51
Oh yes, valid JavaScript, just not JSON. – Quentin Jul 25 '12 at 9:26

Use .php file extension and this will work.

If not then you variable won't have a value, you can see the exact issue by using something like firebug.

share|improve this answer
No, it will not, PHP won't be executed inside strings. – nico Jul 24 '12 at 19:07
4  
@nico — There aren't any strings with PHP in them … unless you mean the JavaScript string (in which case you are wrong, PHP doesn't refuse to enter code mode just because the template has a quote character in it). – Quentin Jul 24 '12 at 19:11
@Quentin: my bad, you are right – nico Jul 24 '12 at 19:14

You say that the file where your code is located has an .html extension, it should be .php for it to render the php code.

share|improve this answer
Of course that is not necessarily true. It depends on the webserver configuration. – nico Jul 24 '12 at 19:15

Well, you can rename the file to .php and it should work or you can do the following thing: Create a .htaccess file, Add the following code ->

RewriteEngine On
<FilesMatch "(file.html)">
    SetHandler php5-script
</FilesMatch>

save the file, then in the html file add the following php line at the beggining -> than you can write php code inside the selected html file

Or you can add the following rule

 RewriteEngine On
 file.html file.php
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.