Home > Web Front-end > JS Tutorial > How to call php from js

How to call php from js

coldplay.xixi
Release: 2020-11-04 11:13:16
Original
5842 people have browsed it

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.

How to call php from js

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>
Copy after login

There is such a PHP code in b.php:

<? 
$action=$_GET[&#39;action&#39;]; 
//echo "var jstext=&#39;$action&#39;"; 
//输出一句JS语句,生成一个JS变量,并赋颠值为PHP变量 $action的值
 //echo "var jstext=&#39;aa&#39;"; echo "var jstext="."&#39;$action&#39;"; 
?>
Copy after login

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!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template