简体   繁体   English

如何在.php a.html 中包含指向具有 php 代码的 a.js 的链接

[英]How to include in .php a .html with links to a .js that has php codes

I have a folder that basically contains:我有一个文件夹,基本上包含:

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

Inside php i need to load.html to display the webpage of course.在 php 里面我当然需要加载 html 来显示网页。 inside the html there is a script tag that refers to the.js file.在 html 内部有一个引用 .js 文件的脚本标签。

Now inside the JS file i have a php code that is needed to run there.现在在 JS 文件中,我有一个需要在那里运行的 php 代码。 But using my methods of loading the html the.js throws an error但是使用我加载 html the.js 的方法会引发错误

PHP PHP

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

HTML HTML

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

Javascript Javascript

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

Unfortunately the way i have included the html thows an error in the.js file不幸的是,我包含 html 的方式在 .js 文件中出现错误

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.有没有其他方法可以包含,以便我能够在 my.js 文件中编写 php 代码。 Unfortunatly im not allowed to change the file extention there can only be one php file.不幸的是,我不允许更改文件扩展名,只能有一个 php 文件。 I separated the javascript and css file to make the code a bit cleaner我分离了 javascript 和 css 文件以使代码更清晰

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.编辑:很多可能似乎是误解,这仍然在服务器中,基本上我想要的是用户收到的网页已经具有 MyNum 的值。 I am initializing the variable before it even gets to the user我什至在变量到达用户之前就对其进行了初始化

In your PHP file, create a global variable containing your JSON in a tag:在您的 PHP 文件中,创建一个包含 JSON 的全局变量:

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

and then reference that variable in your script file with myNum .然后使用myNum在脚本文件中引用该变量。

PHP code runs on the server, the client (the browser in this case) will get only the output of the PHP. PHP 代码在服务器上运行,客户端(本例中为浏览器)将仅获取 PHP 的 output。 In fact, browsers can't execute PHP code.事实上,浏览器无法执行 PHP 代码。
JavaScript runs in the client. JavaScript 在客户端运行。 The browser gets the JavaScript code, and executes it.浏览器获取 JavaScript 代码,并执行它。 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).如果您真的想从 PHP 生成 JS 代码,您唯一能做的就是为 js 文件(test.js -> test.js.php)提供一个.php 结尾。
With this, the file will interpreted as PHP.这样,文件将被解释为 PHP。 The browser gets the result (javascript, that contains the encoded JSON), and everything works well.浏览器得到结果(javascript,包含编码的 JSON),一切正常。 If you want to pass $value from the first PHP to test.js.php, read about GET variables.如果你想将 $value 从第一个 PHP 传递到 test.js.php,请阅读 GET 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM