简体   繁体   English

用Javascript代码获取PHP变量

[英]Getting PHP variable in Javascript code

I'm trying to use PHP variables in Javascript but I couldn't. 我正在尝试在Javascript中使用PHP变量,但不能。 After over 2000 lines of writing different JS functions I was fine avoiding that but now I really needed it. 在编写不同的JS函数超过2000行之后,我可以避免这种情况,但是现在我确实需要它。 I'm a bit lost on all the ways to go about this but nothing really worked. 我在实现此目的的所有方法上都有些迷失,但没有任何效果。 Here is my sequence: 这是我的顺序:

index.html file: index.html文件:

...
<script src="myfunctions.js" />
....

myfunctions.js file: myfunctions.js文件:

....
function test() {
    var x = <?php echo $_conf['user_id'];?>
    console.log(x);
}

I was trying to rename the .js file into .php file and add 我试图将.js文件重命名为.php文件并添加

header("Content-type: text/javascript");

at the beginning - that didn't work. 一开始-没用。 I was trying to make .htaccess file with 我试图用.htaccess文件

AddType application/x-httpd-php .js

But that didn't work either. 但这也不起作用。 I'm probably missing just a tiny thing. 我可能只是想念一点东西。 I just need someone fresh and bright to point it out. 我只需要一个新鲜而聪明的人指出这一点。

您可以在JS代码中执行类似的操作。

var php_var = "<?php echo $_conf['user_id'];?>"

You're not assigning PHP value to a Javascript variable. 您没有将PHP值分配给Javascript变量。 Try: 尝试:

var v = "<?php echo $_conf['user_id'];?>";

Your javascript file should be named as "javascript.php" (just put the name you want, the only important thing is the .php 您的javascript文件应命名为“ javascript.php”(只需输入您想要的名称,唯一重要的是.php

You have an index.php 您有一个index.php

Write in your index.php 写在你的index.php

include("javascript.php");

Then in your javascript.php 然后在您的javascript.php中

<script>
function test(){
     var variable = "<? echo $conf['user_id'] ?>";
     alert(variable);
}
<script>

PS: Yo don't need any header. PS:不需要任何标头。

Since you're doing this via a <script> tag, your PHP script MUST output valid Javascript code, as if you'd literally type your variable assignment in manually. 由于您是通过<script>标记执行此操作的,因此您的PHP脚本必须输出有效的Javascript代码,就好像您要手动键入变量赋值一样。 That means doing something like: 这意味着要执行以下操作:

HTML/JS: HTML / JS:

<script src="myscript.php"></script>

PHP: PHP:

<?php
$myvar = 'foo';
?>

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

Which in the end, will produce somethign that will function exactly as if you'd manually typed in the following: 最后,它将生成somethign,其功能与您在下面手动键入的功能完全相同:

<script>
var myvar = 'foo';
</script>

Note the use of json_encode() . 注意使用json_encode() Using this ensures that whatever you're outputting from PHP will become syntactically valid Javascript. 使用此功能可确保您从PHP输出的任何内容都将成为语法上有效的Javascript。

index.html index.html

 ..
<script src="myfunctions.js.php" />
 ...

myfunctions.js.php myfunctions.js.php

<?php
header('Content-Type: text/javascript');
 ...
?>
var val = <?php echo json_encode($val); ?>;
 ...

Other possible solution is to assign server-side data to attributes in html and read them in javascript. 其他可能的解决方案是将服务器端数据分配给html中的属性,然后使用javascript读取它们。 For example index.html could contain something like this: 例如index.html可能包含以下内容:

<div id="user-profile" data-user-id="<?php echo $conf['user_id']; ?>"></div>

and in js file you can get them while necessary(example with jQuery): 并且在js文件中,您可以在必要时获取它们(以jQuery为例):

var userID = $('#user-profile').attr('data-user-id');

Of course you should adjust your server-side settings to process html files. 当然,您应该调整服务器端设置以处理html文件。

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

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