-3

How do I pass the value of a PHP variable to a javascript in separate file

I have a PHP file: test.php

<?php
$x = 20;
?>

Now I have a separate html file with javascript: test.html

<html>
<head>
<script>
//Here I want to check if value of $x in PHP is 20 and print accordingly
//How do I pass the value of $x from the php file to this html file?
</script>
</head>
</html>
user1035927
  • 1,653
  • 5
  • 17
  • 18
  • The answer you were given is correct, though hardcoding values into javascript listings via PHP echo-ing is usually symptom of a bad general design. If this is just for testing purposes, OK, otherwise I suggest you to explain your project in order to understand if, like I suppose, it can be done in a neater way. – gd1 Nov 15 '11 at 11:34
  • Balanced unnecessary downvote – gd1 Nov 15 '11 at 11:35
  • 2
    @gd1 how is it unnecessary when the OP clearly did not do any research (which is one of the listed dv reasons in the tooltip)? This has been asked and answered often enough before. – Gordon Nov 15 '11 at 11:37
  • The OP clearly has so little knowledge about PHP (he lacks the basics) that he probably couldn't have the ability to find the right words to search with. Instead of downvoting, let's point him/her to a basic PHP tutorial to start with, mark the question as duplicate, and close it. – gd1 Nov 15 '11 at 11:39
  • @gd1 please compare the words in the title of this question to the words in the [search query](http://stackoverflow.com/search?q=pass+php+variable+to+javascript) I provided. Then please explain again why the OP couldnt find the solution. I am all for helping people but I expect [them to do their homework](http://stackoverflow.com/questions/ask-advice) before asking. – Gordon Nov 15 '11 at 11:41

1 Answers1

2

Just do this (presuming that your file has a .php file extension):

var x = <?php echo (int) $x; ?>;
[... more JS code ...]

That, after execution, will leave you with

var x = 20;
if(x > 5) alert("Too large!"); // e.g.

Which is probably what you want. Now you can freely work with your PHP variable in Javascript. Note that, for this to work, the PHP script that generates the HTML must know about the $x variable, so you might want to require it:

require_once("fileThatDefinesX.php");

But as you might know, PHP is executed first (on the server) and then the generated JavaScript is executed on the client, so without technologies such as Ajax or Websockets, it is not possible to communicate with PHP using Javascript in the same manner.

fresskoma
  • 25,481
  • 10
  • 85
  • 128
  • do I need to include the php file in the html file, how does it know which php file to refer? – user1035927 Nov 15 '11 at 11:32
  • @user1035927 It's an html file? can you make it a php, so that your server will actually execute the code? Otherwise this solution _won't_ work – Damien Pirsy Nov 15 '11 at 11:33
  • 1
    you will have to rename your .html file to .php for a php "include" statement to work – jlb Nov 15 '11 at 11:33
  • simply renaming will do the trick, or do I need to convert all the html statements in to php using print? – user1035927 Nov 15 '11 at 11:36
  • 4
    Please quit writing code right now and fully read the PHP basic tutorial from http://php.net/manual/en/tutorial.php . If you don't, you're going to do something nasty --indeed. To start see the "Hello World" here: http://www.php.net/manual/en/tutorial.firstpage.php . This is the best advice you can (and will) be given right now. – gd1 Nov 15 '11 at 11:36