I can access a PHP var with Javascript like this:
我可以像这样使用Javascript访问PHP var:
<?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:
但是如果我想使用一个外部JS文件呢:
<script type="text/javascript" src="externaljs.js"></script>
externaljs.js:
externaljs.js:
alert("color: " + "<?php echo $color; ?>");
11 个解决方案
#1
72
You don't really access it, you insert it into the javascript code when you serve the page.
你没有真正访问它,当你服务页面时,你将它插入到javascript代码中。
However if your other javascript isn't from an external source you can do something like:
但是,如果您的其他javascript不是来自外部源,您可以做以下事情:
<?php
$color = "Red";
?>
<script type="text/javascript">var color = "<?= $color ?>";</script>
<script type="text/javascript" src="file.js"></script>
and then in the file.js use color like so:
然后在文件中。js使用的颜色如下:
alert("color: " + color);
#2
9
You can also access data from php script in Javascript (I'll use jQuery here) like this
您还可以使用Javascript(我将在这里使用jQuery)从php脚本访问数据
Create input hidden field within you php file like this
在php文件中创建这样的输入隐藏字段
<input type="hidden" id="myPhpValue" value="<?php echo $myPhpValue ?>" />
in your javascript file:
在你的javascript文件:
var myPhpValue = $("#myPhpValue").val();
//From here you can the whaterver you like with you js Value
if(myPhpValue != ''){
//Do something here
}
This will do the job as well :)
这也会起到作用:)
#3
7
What I've seen done is let .js files run through the php interpreter. Which I can not recommend.
我看到的是让.js文件在php解释器中运行。我不能推荐。
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.
我所推荐的是通过AJAX获取值,并让PHP文件将值返回到JS文件。这是一个更干净的方法。
#4
4
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
当你意识到这一点时,剩下的就很简单了。您可以在主文件中设置一个js变量,然后包含您的外部js,或者使这个外部js动态,也由PHP生成
#5
4
What you likely want, is called Asynchronous JavaScript and XML (AJAX): http://www.w3schools.com/ajax/default.aspa
您可能需要的是异步JavaScript和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.
基本上,假设能够从客户机JavaScript向服务器上的PHP脚本发送消息。在您给出的示例(externaljs.js)中,脚本通过HTTP询问服务器$color是什么。还可以将脚本标记指向生成所需JavaScript的PHP脚本。这取决于你需要做什么。
It helps to have some understanding of taint checking, data verification, and security ;)
对污点检测、数据验证和安全性有一定的了解。)
#6
3
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.
正如其他人所说,javascript没有访问php变量的权限。但是,它确实可以访问DOM。因此,可以使用php将属性添加到某个页面元素。然后你可以用javascript访问这些属性。
e.g. <div id='apple' class='red'>
is completely available to javascript
#7
1
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.
externaljs。js是一个静态文件。当然它不能访问PHP数据。将PHP数据传递给js文件的惟一方法是通过在PHP脚本中写入文件来物理地修改文件,尽管这充其量是一个混乱的解决方案。
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).
根据Olafur Waage的回答进行编辑:我想编写js文件不是唯一的方法。通过PHP解释器传递js从来没有在我脑海中出现过(这是有原因的)。
#8
1
<script type="text/javascript" src="externaljs.js"></script>
You could change it to
你可以把它改成。
<script type="text/javascript" src="externaljs.php"></script>
And the PHP script could just write JavaScript like that :
PHP脚本可以这样写JavaScript:
<?php
$fruit = "apple";
echo 'var fruit = '.json_encode($fruit);
...
Though using AJAX like said Sepehr Lajevardi would be much cleaner
尽管使用AJAX, Sepehr Lajevardi会更干净。
#9
1
Don solution is good, furthermore if you want to use a php array in an external javascipt this can help you:
Don解决方案很好,此外,如果您想在外部javascript中使用php数组,这可以帮助您:
PHP:
PHP:
<?php
$my_php_array = [];
?>
HTML:
HTML:
<script type="text/javascript"> var my_js_array = <?php echo json_encode($my_php_array);?> ; </script>
<script src = "../public/js/my-external-js.js"></script>
Javasript: (You can now use the array like a normal Javascript array)
(现在可以像使用普通Javascript数组一样使用数组)
my_js_array[0]
my_js_array.length
#10
0
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
您不能这样做,也不要尝试这样做,因为这不是一种推荐的方法,但是您可以将php变量作为函数参数传递给用外部js编写的函数
#11
0
2017-2018 and above solution:
2017 - 2018及以上解决方案:
Since nobody bringed it up yet and I guess no one thought of combining the functions base64_encode
and json_encode
yet, you could even send PHP Array variables like that:
由于还没有人对它进行修改,而且我猜也没有人想过要将base64_encode和json_encode函数结合起来,你甚至可以发送PHP数组变量:
index.php
index . php
<?php
$string = "hello";
$array = ['hi', 'how', 'are', 'you'];
$array = base64_encode(json_encode($array));
Then you could just load your desired js file with the parameter for a query string like this:
然后,您只需将所需的js文件装载到一个查询字符串的参数中,如下所示:
echo '<script type="text/javascript" src="js/main.php?string='.$string.'&array='.$array.'">';
echo '
Then js/main.php
will look like this for example. You can test your variables this way:
然后js /主要。php就像这样。你可以这样测试你的变量:
js/main.php
js / main.php
<?php
if ($_GET['string']) {
$a = $_GET['string'];
}
if ($_GET['array']) {
$b = $_GET['array'];
}
$b = json_decode(base64_decode($b));
echo 'alert("String $a: + '.$a.'");';
echo 'alert("First key of Array $array: + '.$b[0].'");';
exit();
?>
The following will then output when you open your index.php
. So you see, you don't open js/main.php
and you still got the javascript functionality from it.
当您打开index.php时,将输出以下内容。你看,你不打开js/main。还有php的javascript功能。
#1
72
You don't really access it, you insert it into the javascript code when you serve the page.
你没有真正访问它,当你服务页面时,你将它插入到javascript代码中。
However if your other javascript isn't from an external source you can do something like:
但是,如果您的其他javascript不是来自外部源,您可以做以下事情:
<?php
$color = "Red";
?>
<script type="text/javascript">var color = "<?= $color ?>";</script>
<script type="text/javascript" src="file.js"></script>
and then in the file.js use color like so:
然后在文件中。js使用的颜色如下:
alert("color: " + color);
#2
9
You can also access data from php script in Javascript (I'll use jQuery here) like this
您还可以使用Javascript(我将在这里使用jQuery)从php脚本访问数据
Create input hidden field within you php file like this
在php文件中创建这样的输入隐藏字段
<input type="hidden" id="myPhpValue" value="<?php echo $myPhpValue ?>" />
in your javascript file:
在你的javascript文件:
var myPhpValue = $("#myPhpValue").val();
//From here you can the whaterver you like with you js Value
if(myPhpValue != ''){
//Do something here
}
This will do the job as well :)
这也会起到作用:)
#3
7
What I've seen done is let .js files run through the php interpreter. Which I can not recommend.
我看到的是让.js文件在php解释器中运行。我不能推荐。
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.
我所推荐的是通过AJAX获取值,并让PHP文件将值返回到JS文件。这是一个更干净的方法。
#4
4
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
当你意识到这一点时,剩下的就很简单了。您可以在主文件中设置一个js变量,然后包含您的外部js,或者使这个外部js动态,也由PHP生成
#5
4
What you likely want, is called Asynchronous JavaScript and XML (AJAX): http://www.w3schools.com/ajax/default.aspa
您可能需要的是异步JavaScript和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.
基本上,假设能够从客户机JavaScript向服务器上的PHP脚本发送消息。在您给出的示例(externaljs.js)中,脚本通过HTTP询问服务器$color是什么。还可以将脚本标记指向生成所需JavaScript的PHP脚本。这取决于你需要做什么。
It helps to have some understanding of taint checking, data verification, and security ;)
对污点检测、数据验证和安全性有一定的了解。)
#6
3
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.
正如其他人所说,javascript没有访问php变量的权限。但是,它确实可以访问DOM。因此,可以使用php将属性添加到某个页面元素。然后你可以用javascript访问这些属性。
e.g. <div id='apple' class='red'>
is completely available to javascript
#7
1
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.
externaljs。js是一个静态文件。当然它不能访问PHP数据。将PHP数据传递给js文件的惟一方法是通过在PHP脚本中写入文件来物理地修改文件,尽管这充其量是一个混乱的解决方案。
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).
根据Olafur Waage的回答进行编辑:我想编写js文件不是唯一的方法。通过PHP解释器传递js从来没有在我脑海中出现过(这是有原因的)。
#8
1
<script type="text/javascript" src="externaljs.js"></script>
You could change it to
你可以把它改成。
<script type="text/javascript" src="externaljs.php"></script>
And the PHP script could just write JavaScript like that :
PHP脚本可以这样写JavaScript:
<?php
$fruit = "apple";
echo 'var fruit = '.json_encode($fruit);
...
Though using AJAX like said Sepehr Lajevardi would be much cleaner
尽管使用AJAX, Sepehr Lajevardi会更干净。
#9
1
Don solution is good, furthermore if you want to use a php array in an external javascipt this can help you:
Don解决方案很好,此外,如果您想在外部javascript中使用php数组,这可以帮助您:
PHP:
PHP:
<?php
$my_php_array = [];
?>
HTML:
HTML:
<script type="text/javascript"> var my_js_array = <?php echo json_encode($my_php_array);?> ; </script>
<script src = "../public/js/my-external-js.js"></script>
Javasript: (You can now use the array like a normal Javascript array)
(现在可以像使用普通Javascript数组一样使用数组)
my_js_array[0]
my_js_array.length
#10
0
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
您不能这样做,也不要尝试这样做,因为这不是一种推荐的方法,但是您可以将php变量作为函数参数传递给用外部js编写的函数
#11
0
2017-2018 and above solution:
2017 - 2018及以上解决方案:
Since nobody bringed it up yet and I guess no one thought of combining the functions base64_encode
and json_encode
yet, you could even send PHP Array variables like that:
由于还没有人对它进行修改,而且我猜也没有人想过要将base64_encode和json_encode函数结合起来,你甚至可以发送PHP数组变量:
index.php
index . php
<?php
$string = "hello";
$array = ['hi', 'how', 'are', 'you'];
$array = base64_encode(json_encode($array));
Then you could just load your desired js file with the parameter for a query string like this:
然后,您只需将所需的js文件装载到一个查询字符串的参数中,如下所示:
echo '<script type="text/javascript" src="js/main.php?string='.$string.'&array='.$array.'">';
echo '
Then js/main.php
will look like this for example. You can test your variables this way:
然后js /主要。php就像这样。你可以这样测试你的变量:
js/main.php
js / main.php
<?php
if ($_GET['string']) {
$a = $_GET['string'];
}
if ($_GET['array']) {
$b = $_GET['array'];
}
$b = json_decode(base64_decode($b));
echo 'alert("String $a: + '.$a.'");';
echo 'alert("First key of Array $array: + '.$b[0].'");';
exit();
?>
The following will then output when you open your index.php
. So you see, you don't open js/main.php
and you still got the javascript functionality from it.
当您打开index.php时,将输出以下内容。你看,你不打开js/main。还有php的javascript功能。