从外部 javascript 文件访问 PHP var
我可以使用 Javascript 访问 PHP var,如下所示:
<?php
$fruit = "apple";
$color = "red";
?>
<script type="text/javascript">
alert("fruit: " + "<?php echo $fruit; ?>"); // or shortcut "<?= $fruit ?>"
</script>
但是如果我想使用外部 JS 文件:
<script type="text/javascript" src="externaljs.js"></script>
externaljs.js,该怎么办:
alert("color: " + "<?php echo $color; ?>");
I can access a PHP var with Javascript like this:
<?php
$fruit = "apple";
$color = "red";
?>
<script type="text/javascript">
alert("fruit: " + "<?php echo $fruit; ?>"); // or shortcut "<?= $fruit ?>"
</script>
But what if I want to use an external JS file:
<script type="text/javascript" src="externaljs.js"></script>
externaljs.js:
alert("color: " + "<?php echo $color; ?>");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您实际上并没有访问它,而是在提供页面时将其插入到 javascript 代码中。
但是,如果您的其他 javascript 不是来自外部源,您可以执行以下操作:
然后在 file.js 中使用颜色,如下所示:
You don't really access it, you insert it into the javascript code when you serve the page.
However if your other javascript isn't from an external source you can do something like:
and then in the file.js use color like so:
您还可以从 Javascript 中的 php 脚本访问数据(这里我将使用 jQuery),如下所示
在您的 php 文件中创建输入隐藏字段,就像
在您的 javascript 文件中一样:
这也可以完成这项工作:)
You can also access data from php script in Javascript (I'll use jQuery here) like this
Create input hidden field within you php file like this
in your javascript file:
This will do the job as well :)
我所看到的做法是让 .js 文件通过 php 解释器运行。 我不推荐。
我推荐的是通过 AJAX 获取值,然后让 PHP 文件将值返回到 JS 文件。这是一种更干净的方法。
What I've seen done is let .js files run through the php interpreter. Which I can not recommend.
What I do recommend is fetching the values through AJAX and have the PHP file return the value to the JS file. Which is a much cleaner method.
首先,您必须了解没有任何程序实际上可以访问其他程序的变量。
当你意识到这一点时,剩下的就很简单了。
您可以在主文件中设置一个 js 变量,然后包含您的外部 js,或者使该外部 js 动态化,也由 PHP 生成
First of all you have to understand that no program can actually have access to the other program's variable.
When you realize that, the rest is simple.
You can either set up a js variable in the main file and then include your external js, or make this external js dynamic, generated by PHP as well
您可能想要的是异步 JavaScript 和 XML (AJAX): http://www.w3schools .com/ajax/default.aspa
基本上,想象一下能够将消息从客户端 JavaScript 发送到服务器上的 PHP 脚本。在您提供的示例 (externaljs.js) 中,您可以让脚本通过 HTTP 询问服务器 $color 是什么。您还可以将脚本标记指向生成所需 JavaScript 的 PHP 脚本。这取决于您需要做什么。
它有助于了解污染检查、数据验证和安全性;)
What you likely want, is called Asynchronous JavaScript and XML (AJAX): http://www.w3schools.com/ajax/default.aspa
Basically, imagine being able to send messages from the clients JavaScript to your PHP scripts on the server. In the example you gave (externaljs.js), you would have the script ask the server what $color is, via HTTP. You can also point the script tag at a PHP script that generates the JavaScript you want. It depends on what you need to do.
It helps to have some understanding of taint checking, data verification, and security ;)
正如其他人所说,javascript 无法访问 php 变量。然而,它确实可以访问 DOM。因此,您可以使用 php 向某些页面元素添加属性。然后你可以使用 javascript 访问这些属性。
例如
完全可用于 javascript
As the others are saying, javascript doesn't have access to php variables. However, it does have access to the DOM. So, you can use php to add attributes to some page element. And then you can access those attributes with javascript.
e.g.
<div id='apple' class='red'>
is completely available to javascriptDon 解决方案很好,此外,如果您想在外部 javascipt 中使用 php 数组,这可以帮助您:
PHP:
HTML:
Javasript: (您现在可以像普通的 Javascript 数组一样使用该数组)
Don solution is good, furthermore if you want to use a php array in an external javascipt this can help you:
PHP:
HTML:
Javasript: (You can now use the array like a normal Javascript array)
externaljs.js 是一个静态文件。当然它不能访问PHP数据。将 PHP 数据传递到 js 文件的唯一方法是通过在 PHP 脚本中写入来物理更改该文件,尽管这充其量是一个混乱的解决方案。
编辑以回应 Ólafur Waage 的回答:我想写入 js 文件并不是唯一的方法。我从来没有想过通过 PHP 解释器传递 js(有充分的理由)。
externaljs.js is a static file. Of course it can't access PHP data. The only way to pass PHP data to a js file would be to physically alter the file by writing to it in your PHP script, although this is a messy solution at best.
Edit in response to Ólafur Waage's answer: I guess writing to the js file isn't the only way. Passing the js through the PHP interpreter never crossed my mind (for good reason).
你可以将其更改为
PHP 脚本可以像这样编写 JavaScript :
虽然像 Sepehr Lajevardi 那样使用 AJAX 会更干净
You could change it to
And the PHP script could just write JavaScript like that :
Though using AJAX like said Sepehr Lajevardi would be much cleaner
2017-2018及以上解决方案:
由于还没有人提出它,而且我猜还没人想到将函数
base64_encode
和json_encode
结合起来,你甚至可以像这样发送 PHP 数组变量:然后,您可以使用查询字符串的参数加载所需的 js 文件,如下所示:
echo '
那么
js/main.php
将如下所示。您可以这样测试您的变量:当您打开
index.php
时,将输出以下内容。所以你看,你没有打开 js/main.php ,你仍然可以从中获得 javascript 功能。2017-2018 and above solution:
Since nobody bringed it up yet and I guess no one thought of combining the functions
base64_encode
andjson_encode
yet, you could even send PHP Array variables like that:Then you could just load your desired js file with the parameter for a query string like this:
echo '<script type="text/javascript" src="js/main.php?string='.$string.'&array='.$array.'">';
Then
js/main.php
will look like this for example. You can test your variables this way:The following will then output when you open your
index.php
. So you see, you don't openjs/main.php
and you still got the javascript functionality from it.您可以像使用其他任何东西一样
include()
它们:这基本上会将外部文件渲染为内联 js。这里的主要缺点是您失去了浏览器缓存的潜在性能优势。另一方面,它比在 javascript 中重新声明 php 变量要容易得多。
You can
include()
them just as you would anything else:This will basically render the external file as inline js. The main disadvantage here is that you lose the potential performance benefit of browser caching. On the other hand, it's much easier than re-declaring your php variables in javascript.
你不能这样做,也不要尝试这样做,因为这不是推荐的方法,但是你可以将 php 变量作为函数参数传递给用外部 js 编写的函数
You cant do that and dont try to as this is not a recommended approach, However you can pass php variables as a function parameters to function written in external js