js call php and PHP call JS method, small series to make the following summary, hope to help everyone;
First: JS method to call the PHP file and get the value in PHP , the following examples:
such as in the page test_1 with the following sentence call:
<!--example, this JS file does not exist, the JS file is changed to your own--><script type= "Text/javascript" src= "Http://www.php.cn/a.js" ></ Script><script type= "Text/javascript" > alert (jstext);</script>
test. php File:
<?php$php_test= ' I like topic.alibabacloud.com! '; echo "var test= ' $php _test ';"; echo "var jstext=". "' $php _test ';? >
When the test. php file is executed, the test_1. php file is called and the output of the b.php file is executed as a JS statement.
So here will be a popup box, the contents of the JS variable jstext value, that is, the PHP file assigned to Jstext value.
Summary: In the HTML with JS call file in the way the PHP file, the output of the PHP file will be called the page as JS code to use.
PHP calls the value in JS
There is a piece of code in the test.php page:
<script type= "Text/javascript" > var data= "call_me_why"; </script><? echo "<script type=text/javascript>document.write (data) </script>";? >
PHP calls the method (function) in JS
Similar to the second case, using the Echo script to implement the JS call
<script type= "Text/javascript" > Function test () { var t1=3; T1 = t1+2; alert (t1); return t1; } </script><?php echo "<script type= ' Text/javascript ' >test ();</script>";?>
JS Call PHP variable (1)
<?php$userid=100;? > <script>var Userid;userid=document.getelementbyid ("userid"). Value;alert (userid);</script>< Input type= "text" name= "userid" id= "userid" value= "<?php echo $userId;?>" >
(2)
<?php $url = ' call_me_why '; Define variables? ><script type= "Text/javascript" > //js call php variable var ds = "<?php echo $url?>";//Assignment Alert (DS); Output Effects </script>
JS Call php function
<script language= "JavaScript" >var y=<?php echo Date (' Y ')? >,m=<?php echo Date (' n ')? >,d=<?php Echo Date (' J ')? >;alert (Y); alert (M); alert (D);</script>