不能在JavaScript中使用我的php变量

fis*_*g3r 0 javascript php

我有一个php变量.然后在按钮上点击JS,我必须打开一个包含该变量的链接.根据其他主题,这必须起作用,但事实并非如此.如果点击它就没有任何反应.

<?php
    $id = 'string';
?>
<SCRIPT language="JavaScript">
   function openBox() {
       php_var = <?php echo $id; ?>;
       targetURL = "other_page.php?id=" + php_var
       theBox = window.open(targetURL, 'theResults', 'status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=1, width=600,height=680,left=10,top=10');
   }
</SCRIPT>
Run Code Online (Sandbox Code Playgroud)

*编辑:好了,我忘了var,';.修改后的代码(如下所示)现在打开所需的站点,但var为空.如果我只是在php中回显它,它就有它的价值.

<?php
    $id = 'string';
?>
<SCRIPT language="JavaScript">
   function openBox() {
       php_var = '<?php echo $id; ?>';
       targetURL = "other_page.php?id=" + php_var
       theBox = window.open(targetURL, 'theResults', 'status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=1, width=600,height=680,left=10,top=10');
   }
</SCRIPT>
Run Code Online (Sandbox Code Playgroud)

T00*_*0rk 5

你需要添加引号和 var

var php_var = '<?php echo $id; ?>';
var targetURL = "other_page.php?id=" + php_var
var theBox = window.open(targetURL, 'theResults', 'status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=1, width=600,height=680,left=10,top=10');
Run Code Online (Sandbox Code Playgroud)