如何使用 PHP echo 函数在 JavaScript 中定义变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13734338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define a variable in JavaScript with PHP echo function?
提问by HellaMad
How do I define a variable in javascript with echo function, from the external php file?
如何从外部 php 文件中使用 echo 函数在 javascript 中定义变量?
We have theconfigfile.php, thejsfile.jsand thephpfile.php.
我们有theconfigfile.php,thejsfile.js和thephpfile.php。
In theconfigfile.phpwe have:
在configfile.php中,我们有:
<?php
$path = 'http://example.com/home.php'; // Set your path
?>
In thejsfile.jswe have:
在jsfile.js我们有:
...
if (confirm("Are you sure you want to delete")) {
$.ajax({
type: "POST",
url: "http://example.com/home.php",
data: dataString,
cache: false
});
...
And in thephpfile.phpwe have:
在phpfile.php 中,我们有:
<php
include "theconfigfile.php";
?>
<html>
<head>
<script type="text/javascript" src="thejsfile.js"></script>
</head>
<body>
...here is the code that uses file thejsfile.js...
</body>
</html>
I used this method:
我用了这个方法:
...
if (confirm("Are you sure you want to delete")) {
$.ajax({
type: "POST",
url: "<?php echo $path; ?>",
data: dataString,
cache: false
});
...
Only works when javascript is part of the code. And if I use it external, like this...
仅当 javascript 是代码的一部分时才有效。如果我在外部使用它,就像这样......
<script type="text/javascript" src="thejsfile.js"></script>
...does not work! Solutions?
...不起作用!解决方案?
回答by Mike Brant
There are a couple of ways you can go about doing this.
有几种方法可以做到这一点。
You can configure your webserver to process files with extension .js
with PHP and just inject your PHP there. Of course this means you need a way to actually calculate your variable there, and this would slow down serving your regular javascript content.
您可以配置您的网络服务器以处理.js
带有 PHP扩展名的文件,然后将您的 PHP 注入其中。当然,这意味着您需要一种方法来实际计算您的变量,这会减慢为您的常规 javascript 内容提供服务的速度。
You can simply output the PHP variable to a Javascript variable within a <script>
element like this
您可以简单地将 PHP 变量输出到<script>
这样的元素中的 Javascript 变量
<script type="text/javascript">
var path = "<?php echo $path; ?>";
</script>
And then access this path
variable in your AJAX. Most would probably use the second approach.
然后path
在你的 AJAX 中访问这个变量。大多数人可能会使用第二种方法。
回答by HellaMad
You can rename your thejsfile.js
to thejsfile.php
, add the following to the very beginningof it, and it should be parsed for PHP:
你可以重命名你的thejsfile.js
to thejsfile.php
,在它的最开始添加以下内容,它应该被解析为 PHP:
<?php header("Content-type: text/javascript"); ?>
Then reference it like this:
然后像这样引用它:
<head>
<script type="text/javascript" src="thejsfile.php"></script>
</head>
Your other option is to just set your server up to parse .js
files for PHP.
您的另一个选择是将您的服务器设置为解析.js
PHP 文件。
回答by shapeshifter
In thephpfile.php,
在phpfile.php中,
<html>
<head>
<script type="text/javascript">
var config = { url: '<?= $path; ?>' };
</script>
<script type="text/javascript" src="thejsfile.js"></script>
</head>
Then you can access it with config.url in your javascript.
然后你可以在你的 javascript 中使用 config.url 访问它。
回答by zavg
You can put values in such way only into the files which are processed by PHP.
External JavaScript-files are not processed by PHP so they are linked as is without sustitution.
您只能以这种方式将值放入由 PHP 处理的文件中。
PHP 不处理外部 JavaScript 文件,因此它们按原样链接而无需替换。
回答by Jason
Unless you're making a post to another server via JSONP then you might just consider using a relative path hard-coded in your Javascript that way you don't need to send the path from the server to the client:
除非您通过 JSONP 向另一台服务器发帖,否则您可能只考虑在 Javascript 中使用硬编码的相对路径,这样您就不需要将路径从服务器发送到客户端:
...
$.ajax({
type: "POST",
url: "home.php",
data: dataString,
cache: false
});
....
But, if this is a URL that changes frequently and it really does need to be configurable then you can echo out the PHP variable as script something like this"
但是,如果这是一个经常更改的 URL 并且它确实需要可配置,那么您可以将 PHP 变量作为类似这样的脚本回显”
<html>
...
<script src="thejsfile.js" type="text/javascript">
<script type="text/javascript">
// 'path' is a variable that is defined in thejsfile.js
path = '<?php echo htmlentities($path) ?>';
</script>
...
Whenever you output info from the server to the client (especially as Javascript) you have to be REALLY careful that it is escaped to prevent scripting attacks (ie allowing people to inject javascript into your code). In this case there is no reason to allow any type of html characters.
每当您将信息从服务器输出到客户端(尤其是作为 Javascript)时,您必须非常小心地将其转义以防止脚本攻击(即允许人们将 javascript 注入您的代码)。在这种情况下,没有理由允许任何类型的 html 字符。
回答by lukek
When working with external JS files You could store the value in a hidden field and retrieve the fields value with JavaScript.
使用外部 JS 文件时,您可以将值存储在隐藏字段中并使用 JavaScript 检索字段值。
<input type="hidden" name="path" id="path" value="<?php echo $path; ?>" />
and in the javascript (i'll use jquery)
并在 javascript 中(我将使用 jquery)
var path = $('#path').attr('value');
Obviously you could write the variable to the script tags on the page, but if you use any advanced type of patterns it may be hard to access that variable.
显然,您可以将变量写入页面上的脚本标签,但如果您使用任何高级类型的模式,可能很难访问该变量。
回答by Black Mamba
echo "<script type='text/javascript'>var AccomodationInfoForm4 =".$path."</script>";
Just try to echo the way shown above and you can access your variable anywhere you want.As this will be available globally to all your js
files on your page.
只需尝试响应上面显示的方式,您就可以在任何地方访问您的变量。因为这将对js
您页面上的所有文件全局可用。