简体   繁体   English

将变量从JavaScript传递到PHP代码不起作用

[英]Passing variables from JavaScript to PHP code doesn't work

I am trying to pass 2 variables from PHP to JavaScript and the vice versa. 我试图将2个变量从PHP传递到JavaScript,反之亦然。

I understand how to pass one variable, and it's okay, but when I tried to pass 2 variales, one of them passed, the other one didn't work. 我知道如何传递一个变量,这没关系,但是当我尝试传递2个变量时,其中一个通过了,另一个则不起作用。

Here is the code: 这是代码:

(1) file1.php: (1)file1.php:

<script type="text/javascript"> 
var id = <?php echo $id ; ?>
var q= <?php echo $q ; ?>
</script>
<script type="text/javascript"src="http://localhost/site/js/java.js">      </script>

(2) Java.js: (2)Java.js:

http://localhost/site/file2.php?id="+id +"&q="+q

(3) file2.php: (3)file2.php:

 $id = $_GET['id']; >> it works fine
 $q= $_GET['q']; >> doesn't pass (error: undefined)

I am not sure where the problem is, I just think it's in the number (2) step. 我不确定问题出在哪里,我只是认为问题出在第二步。

Any help will be appreciated. 任何帮助将不胜感激。

As per comments, if q is a string then it needs to be enclosed in quotes. 根据注释,如果q是字符串,则需要将其用引号引起来。 Remember all you php code is doing is generating text. 记住,您的php代码所做的就是生成文本。

var q= <?php echo $q ; ?>

Should be 应该

var q= "<?php echo $q ; ?>";

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

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