-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>
5
  • 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. Commented Nov 15, 2011 at 11:34
  • Balanced unnecessary downvote Commented Nov 15, 2011 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. Commented Nov 15, 2011 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. Commented Nov 15, 2011 at 11:39
  • @gd1 please compare the words in the title of this question to the words in the search query 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 before asking. Commented Nov 15, 2011 at 11:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

do I need to include the php file in the html file, how does it know which php file to refer?
@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
you will have to rename your .html file to .php for a php "include" statement to work
simply renaming will do the trick, or do I need to convert all the html statements in to php using print?
Please quit writing code right now and fully read the PHP basic tutorial from 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: php.net/manual/en/tutorial.firstpage.php . This is the best advice you can (and will) be given right now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.