1. Call the PHP file in JS mode and obtain the value in PHP.
Here is a simple example:
For example, call the following statement in test_json1:
<script type="text/javascript" src="http://callmewhy.sinaapp.com/index.php/test/testjson2"></script><script type="text/javascript" > alert(jstext);</script>
There is such a PHP code in test_json2.php:
<?php$php_test='I come from PHP!';echo "var test='$php_test';";echo "var jstext="."'$php_test';"; ?>
When the test_json1.php file is executed, the test_json2.php file is called and the output of the B. php file is executed as a JS statement,
Therefore, a prompt box is displayed, containing the value of the JS variable jstext, that is, the value assigned to jstext in the PHP file.
Summary: when PHP files are called by JS in HTML, the output of PHP files will be called by pages as JS Code.
2. php calls the value in JS
The test_json3.php page contains the following code:
<script type="text/javascript" > var data="call_me_why"; </script><? echo "<script type=text/javascript>document.write(data)</script>";?>
3. php calls methods (functions) in JS)
Similar to the second scenario, the echo script is used to implement JS calls.
<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>"; ?>
4. js calls PHP Variables
(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 the variable?> <SCRIPT type = "text/JavaScript"> // JS calls the PHP variable var ds = "<? PHP echo $ URL?> "; // Assign a value to alert (DS); // output result </SCRIPT>
5. js calls PHP Functions
<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>