0

I have a folder that basically contains:

  • .php
  • .html
  • .js
  • .css

Inside php i need to load .html to display the webpage of course. inside the html there is a script tag that refers to the .js file.

Now inside the JS file i have a php code that is needed to run there. But using my methods of loading the html the .js throws an error

PHP

<?php
  $value = 1;
  //$html = file_get_html('index.html')
  //include ("index.html")
  readfile('index.html');
?>

HTML

<html>
  <head>
    <script src="script.js"></script>
  </head>
  <body>
  </body>
</html>

Javascript

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

Unfortunately the way i have included the html thows an error in the .js file

Uncaught SyntaxError: Unexpected token '<'

What am i doing wrong? Are there any other way to include so that i will be able to write php code in my .js file. Unfortunatly im not allowed to change the file extention there can only be one php file. I separated the javascript and css file to make the code a bit cleaner

EDIT: A lot may seem to be misunderstanding, This is still hapening in the server, basically what i want is that the webpage recieved by the user already has the value for MyNum. I am initializing the variable before it even gets to the user

DrakeJest
  • 225
  • 3
  • 13
  • 2
    PHP is not run in a JS file. – Teemu Apr 28 '20 at 19:15
  • You can't run PHP in a javascript file. – MARSHMALLOW Apr 28 '20 at 19:15
  • If you want the PHP code to be processed, you'll need to give the file a .php extension, or change the server configuration. You could output a bit of html in your PHP code that creates a placeholder div with the value in it, and then have your JS read it from there. – droopsnoot Apr 28 '20 at 19:16
  • In your PHP file, create a global variable containing your JSON in a ` – chriskirknielsen Apr 28 '20 at 19:17
  • @Teemu wait okay just making sure we are not misunderstanding here, This is happening still in server side okay, not yet passed to the user – DrakeJest Apr 28 '20 at 19:24
  • Does this answer your question? [how to execute php code within javascript](https://stackoverflow.com/questions/12498839/how-to-execute-php-code-within-javascript) – SuperDJ Apr 28 '20 at 19:24
  • No misunderstanding, PHP doesn't know anything about the JS file, it just prints the tag to the markup it is building. Browser will load the JS after PHP has responsed the request with a HTML string. – Teemu Apr 28 '20 at 19:26
  • @Teemu what i was hoping that when the user recieve the html (which then runs the JS file) the variable has already been set – DrakeJest Apr 28 '20 at 19:30
  • Yes, I can understand what you need, but it's not possible with an external JS file (unless you can implement Zoldszemesostoros' test.js -> test.js.php solution) . You've to follow the tips given in the comments. – Teemu Apr 28 '20 at 19:30
  • @Teemu i see, from what i understand from the other comments is, if it isnt a .php it wont be executed by the server, is that correct? – DrakeJest Apr 28 '20 at 19:33
  • @chriskirknielsen i like that idea to eco it inside the php, ill give it a try. – DrakeJest Apr 28 '20 at 19:33
  • Exactly taken, only PHP files are executed by PHP, but everything is handled on some way on the server, it couldn't otherwise deliver the requested files. – Teemu Apr 28 '20 at 19:34
  • @Teemu is possible to call a script tag outside an html ? i tried doing this: `echo "";` followed by `readfile('index.html');` but a console.log on the .js results in undefined unfotunatly – DrakeJest Apr 28 '20 at 19:47
  • Yes, that should work (providing the commas are typos only in the comment), and is widely used. – Teemu Apr 28 '20 at 19:50
  • @Teemu apologies those are supposed to be dots, that results to an undefined myNum though – DrakeJest Apr 28 '20 at 19:54
  • Found the problem its with the json_encode. @chriskirknielsen post your comment as an answer i will accept it, your idea worked for me. – DrakeJest Apr 28 '20 at 20:02
  • @DrakeJest Sure thing! Glad you got it figured out. – chriskirknielsen Apr 28 '20 at 20:24

2 Answers2

3

In your PHP file, create a global variable containing your JSON in a tag:

<script>var myNum = <?php echo json_encode($value); ?>;</script>

and then reference that variable in your script file with myNum.

chriskirknielsen
  • 2,839
  • 2
  • 14
  • 24
0

PHP code runs on the server, the client (the browser in this case) will get only the output of the PHP. In fact, browsers can't execute PHP code.
JavaScript runs in the client. The browser gets the JavaScript code, and executes it. The only thing you can do if you really want to produce JS code from PHP is to give a .php ending for the js file (test.js -> test.js.php).
With this, the file will interpreted as PHP. The browser gets the result (javascript, that contains the encoded JSON), and everything works well. If you want to pass $value from the first PHP to test.js.php, read about GET variables.

Zoldszemesostoros
  • 387
  • 1
  • 3
  • 15
  • 1
    Doesn't PHP treat that JS like HTML, `<` and many more special characters are replaced with HTMLEntities, and the entire script is wrapped within tags ..? – Teemu Apr 28 '20 at 19:23
  • What i want is actually to initialize the javascript variable before even getting it to the user – DrakeJest Apr 28 '20 at 19:27