P粉1119279622023-08-23 15:50:54
To determine why PHP code is not working within JavaScript code, we need to understand what client-side and server-side are Languages, and how they work.
Server-side languages (such as PHP, etc.) : They retrieve records from the database, maintain state on a stateless HTTPconnection, and execute Many operations require security. They reside on the server and the source code of these programs is never exposed to users.
So you can easily see that the server side language handles the HTTP request and processes it, as @deceze said, PHP executes on the server and outputs some HTML, and possibly JavaScript code, which The response is sent to the client, where the HTML is interpreted and JavaScript executed.
On the other hand, Client-side languages (such as JavaScript) reside in and run in the browser. Client-side script Generally refers to a computer program that is executed on the Web by the user's Web browser rather than the server side.
JavaScript is visible to the user and can be easily modified, so for security issues we cannot rely on JavaScript.
So when you make a HTTP request on the server, the server first carefully reads the PHP file to see if there are any tasks that need to be performed, and sends a response to the client. Again, as @deceze said, *once PHP has finished outputting the response, the script will end and nothing will happen on the server until a new HTTP request is received. *
So, what should I do if I need to call PHP? It depends on how you need to do it: by reloading the page or using an AJAX call.
Good reading materials:
P粉1037395662023-08-23 09:17:40
Your code is split into two completely separate parts, server-side and client-side.
|
---------->
HTTP请求
|
+--------------+ | +--------------+
| | | | |
| 浏览器 | | | 网络服务器 |
| (JavaScript) | | | (PHP等) |
| | | | |
+--------------+ | +--------------+
|
客户端 | 服务器端
|
<----------
HTML, CSS, JavaScript
|
The two parts communicate via HTTP requests and responses. PHP executes on the server and outputs some HTML and possibly JavaScript code, which is sent as a response to the client, where it interprets the HTML and executes the JavaScript. Once PHP finishes outputting the response, the script ends and nothing happens on the server until a new HTTP request comes in.
The execution process of the sample code is as follows:
<script type="text/javascript"> var foo = 'bar'; <?php file_put_contents('foo.txt', ' + foo + '); ?> var baz = <?php echo 42; ?>; alert(baz); </script>
In the first step, PHP executes all the code between the <?php ?>
tags. The results are as follows:
<script type="text/javascript"> var foo = 'bar'; var baz = 42; alert(baz); </script>
file_put_contents
The call produces no results, it just writes "foo" to the file. And <?php echo 42; ?>
The result of the call is to output "42", which is now at the location of the original code.
The resulting HTML/JavaScript code is now sent to the client, where it is evaluated. The alert
call works and the foo
variable is not used anywhere.
All PHP code is executed on the server before the client starts executing any JavaScript. There is no PHP code left in the response to interact with JavaScript.
To call some PHP code, the client must send a new HTTP request to the server. This can be achieved in one of three possible ways:
Here is an issue outlining these methods in more detail
You can also use JavaScript to cause the browser to open a new page, using window.location
or submit a form, simulating possibilities 1 and 2.