【转】PHP----JS相互调用

JS调用PHP

1.取值: 执行html,得到一个弹窗,提示:I from PHP

 

[javascript] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <script type="text/javascript" src="http://127.0.0.1:8080/wp_php/index1.php">  </script>    
  2. <script type="text/javascript" >    
  3.     alert(jsTest);    
  4. </script>   

 

 

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. $php_test='I from PHP';  
  3. echo "var jsTest="."'$php_test';";  
  4. ?>  

 

 

2.取值(变量)JS 要在input 后,不然js取不到值 

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <html>  
  2. <body>  
  3.     <?php   
  4.         $userCar = 525;  
  5.     ?>  
  6.     <input type="text" id="userCar" value="<?php echo $userCar ?>" />  
  7.       
  8.     <script type="text/javascript">  
  9.         alert(document.getElementById("userCar").value);  
  10.     </script>  
  11.       
  12. </body>  
  13. </html>  



 

3.取值,注意使用引号(PHP的string,int...)

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <html>  
  2. <body>  
  3.     <?php   
  4.         $userCar = 525;  
  5.     ?>     
  6.     <script type="text/javascript">  
  7.         var userCar = '<?php echo $userCar ?>';  
  8.         alert(userCar);  
  9.     </script>  
  10.       
  11. </body>  
  12. </html>  

 

 

4.调用方法(函数)

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <html>  
  2. <body>  
  3.     <script type="text/javascript">  
  4.         alert (<?php echo date("Y")?>);  
  5.     </script>  
  6. </body>  
  7. </html>  



 

 

PHP调用JS

1.取值:     显示:values;

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <html>  
  2. <body>  
  3.     <script type="text/javascript">  
  4.         var str = 'values';  
  5.     </script>  
  6.     <?php   
  7.         echo "<script type=text/javascript>document.write(str)</script>";  
  8.     ?>  
  9. </body>  
  10. </html>  



 

2.调用函数(方法)    弹窗:9

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <html>  
  2. <body>  
  3.     <script type="text/javascript">  
  4.         function add(){  
  5.             var x = 0;  
  6.             x = x + 9;  
  7.             alert(x);  
  8.         }  
  9.     </script>  
  10.     <?php   
  11.         echo "<script type=text/javascript>add()</script>";  
  12.     ?>  
  13. </body>  
  14. </html>  


其实一点:那里调用,那里就有echo ;

from:http://blog.csdn.net/damys/article/details/29807893

posted on 2017-04-21 11:15  神奇的旋风  阅读(13795)  评论(0编辑  收藏  举报

导航