JS调用PHP
1.取值: 执行html,得到一个弹窗,提示:I from PHP
- <script type="text/javascript" src="http://127.0.0.1:8080/wp_php/index1.php"> </script>
- <script type="text/javascript" >
- alert(jsTest);
- </script>
- <?php
- $php_test='I from PHP';
- echo "var jsTest="."'$php_test';";
- ?>
2.取值(变量)JS 要在input 后,不然js取不到值
- <html>
- <body>
- <?php
- $userCar = 525;
- ?>
- <input type="text" id="userCar" value="<?php echo $userCar ?>" />
- <script type="text/javascript">
- alert(document.getElementById("userCar").value);
- </script>
- </body>
- </html>
3.取值,注意使用引号(PHP的string,int...)
- <html>
- <body>
- <?php
- $userCar = 525;
- ?>
- <script type="text/javascript">
- var userCar = '<?php echo $userCar ?>';
- alert(userCar);
- </script>
- </body>
- </html>
4.调用方法(函数)
- <html>
- <body>
- <script type="text/javascript">
- alert (<?php echo date("Y")?>);
- </script>
- </body>
- </html>
PHP调用JS
1.取值: 显示:values;
- <html>
- <body>
- <script type="text/javascript">
- var str = 'values';
- </script>
- <?php
- echo "<script type=text/javascript>document.write(str)</script>";
- ?>
- </body>
- </html>
2.调用函数(方法) 弹窗:9
- <html>
- <body>
- <script type="text/javascript">
- function add(){
- var x = 0;
- x = x + 9;
- alert(x);
- }
- </script>
- <?php
- echo "<script type=text/javascript>add()</script>";
- ?>
- </body>
- </html>
其实一点:那里调用,那里就有echo ;
from:http://blog.csdn.net/damys/article/details/29807893