简体   繁体   English

如何将变量从php传递到jQuery

[英]How pass variable from php to jQuery

I have a config.php file where I define several variables. 我有一个config.php文件,其中定义了几个变量。

I want to use some of those variables in a jQuery script. 我想在jQuery脚本中使用其中一些变量。

I created a header-js.php file with the following content: 我创建了带有以下内容的header-js.php文件:

<script type="text/javascript">
jQuery("#msgid1").html("Hello world.");
jQuery("#msgid1").html("Hello world again.");
global_cookie_prefix = <?php echo(global_cookie_prefix);?>;
</script>

When I run the above the msgid1 div shows "Hello world again." 当我在上面运行时,msgid1 div显示“ Hello world again”。

When I swap the lines to: 当我交换行到:

<script type="text/javascript">
jQuery("#msgid1").html("Hello world.");
global_cookie_prefix = <?php echo(global_cookie_prefix);?>;
jQuery("#msgid1").html("Hello world again.");
</script>

the msgid1 div shows "Hello world." msgid1 div显示“ Hello world”。

It seems that the line where I am defining global_cookie_prefix is causing the script to abort. 似乎我在定义global_cookie_prefix的那一行导致脚本中止。

I am at a loss on how to solve this. 我不知道如何解决这个问题。

Thaks. 解冻。

I'd bet the problem is that you're not defining your variable with the var keyword, and/or that your PHP snippet is not between quotes. 我敢打赌,问题在于您没有使用var关键字定义变量,和/或您的PHP代码段不在引号之间。 Try this 尝试这个

var global_cookie_prefix = '<?php echo global_cookie_prefix; ?>';
^^^                        ^                                   ^

As you want to pass the variables to JS as strings, you will need to tell JS it's strings: global_cookie_prefix = '<?php echo global_cookie_prefix;?>'; 当您要将变量作为字符串传递给JS时,您需要告诉JS它是字符串: global_cookie_prefix = '<?php echo global_cookie_prefix;?>'; (assuming global_cookie_prefix is a constant, in which case it should be all uppercase by convention) (假设global_cookie_prefix是一个常量,在这种情况下,按照惯例,它应该全部为大写)

are you sure global_cookie_prefix is a constant (because its not uppercase). 您确定global_cookie_prefix是一个常量(因为它不是大写)。 Try adding the variable sign '$' to global_cookie_prefix in case its not a defined constant. 尝试将变量符号“ $”添加到global_cookie_prefix,以防其未定义常量。

<?php echo($global_cookie_prefix);?>;

also you need quotes in your javascript... (try with and without $ depending on constant or not) btw by convention constants are uppercase. 此外,您还需要在JavaScript中加引号...(按$尝试使用和不使用$取决于常量),顺便说一句,常量是大写的。

global_cookie_prefix = "<?php echo global_cookie_prefix; ?>";

Since your script is actually a PHP script outputting Javascript, your script may be terminating on an error because global_cookie_prefix is an undefined constant. 由于您的脚本实际上是输出Javascript的PHP脚本,因此您的脚本可能会因错误而终止,因为global_cookie_prefix是未定义的常量。 If global_cookie_prefix is a variable and you have it defined somewhere else on the page where this script is included, you might want to update your code to below by adding the '$' sign. 如果global_cookie_prefix是变量,并且已在包含此脚本的页面上的其他位置定义了该变量,则可能需要通过添加“ $”符号将代码更新为以下内容。

You also need the 'var' keyword in your JS to avoid errors in strict mode. 您还需要在JS中使用'var'关键字,以避免在严格模式下出错。

<script type="text/javascript">
  jQuery("#msgid1").html("Hello world.");
  **var** global_cookie_prefix = '<?php echo(**$**global_cookie_prefix);?>';
  jQuery("#msgid1").html("Hello world again.");
</script>

I would also recommend checking your apache error logs to see what the error and confirm that your PHP output is not breaking on that line. 我还建议您检查您的apache错误日志,以查看错误原因,并确认您的PHP输出未在该行中断。

Also, if you have error_reporting on, the PHP generated warning about global_cookie_prefix being undefined would definitely break your javascript and make it fail at that point. 另外,如果您打开error_reporting,PHP会生成有关global_cookie_prefix未定义的警告,这肯定会破坏您的JavaScript,并使其在此时失效。

It would be easier to help if you pasted more of your code to give this snippet context. 如果您粘贴更多代码以提供此代码段上下文,则将更容易获得帮助。

EDIT: I added single quotes around the PHP output since a string output from the PHP would break your JavaScript. 编辑:我在PHP输出周围添加了单引号,因为PHP的字符串输出会破坏您的JavaScript。

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

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