Home > Backend Development > PHP Tutorial > 如何在JS中得到PHP的变量值?

如何在JS中得到PHP的变量值?

WBOY
Release: 2016-06-23 14:14:54
Original
955 people have browsed it

比如是这样:


  
  <script></script>



这样的话xx.js中插入好像引用不到那个$name变量,请问哪里出问题了?

回复讨论(解决方案)

xx.js中不能用php的语法,你可以通过xx.js进行传参
例如<script>" ></script>

xx.js中不能用php的语法,你可以通过xx.js进行传参
例如<script>" ></script>
那我再xx.js中如何引用到这个变量呢?


写作
<script> <br /> <?php echo "name = '$_POST[name];" ?> <br /> </script>
这样在 xx.js 中就可以使用变量 name 了


写作
<script> <br /> <?php echo "name = '$_POST[name];" ?> <br /> </script>
这样在 xx.js 中就可以使用变量 name 了
在xx.js中直接引用name变量么?我试了一下好像不行 说没定义
另外我看还有人这样写的:

<script> <br /> var name="&name=<?php echo $name; ?>"; <br /> <br /> </script>
请问这个和您说的方法一样么




  
  <script>var name = "<?php echo $name; ?>";</script>
  <script></script>



  
  <script>var name = "<?php echo $name; ?>";</script>
  <script></script>


THX!
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template