JS method of calling php: First call the relevant statements in the page [a.html], and add the relevant code in [b.php]; then when the [a.html] file is executed, it will Call the [b.php] file and execute the output of the [b.php] file as a JS statement.
JS method of calling php:
Give a simple example to illustrate:
For example, in the page a.html, use the following sentence to call:
<script type="text/javascript" src="b.php?action=test"></script> <script type="text/javascript" > alert(jstext); </script>
There is such a PHP code in b.php:
<? $action=$_GET['action']; //echo "var jstext='$action'"; //输出一句JS语句,生成一个JS变量,并赋颠值为PHP变量 $action的值 //echo "var jstext='aa'"; echo "var jstext="."'$action'"; ?>
When the a.html file is executed, b will be called .php file, and execute the output of the b.php file as a JS statement, so a prompt box will pop up here with the value of the JS variable jstext, which is the value assigned to jstext in the PHP file.
Related free learning recommendations: javascript (video)
The above is the detailed content of How to call php from js. For more information, please follow other related articles on the PHP Chinese website!