I have this code:
我有这段代码:
<script type="text/javascript">
var foo = 'bar';
<?php
file_put_contents('foo.txt', ' + foo + ');
?>
var baz = <?php echo 42; ?>;
alert(baz);
</script>
Why does this not write "bar" into my text file, but alerts "42"?
为什么不把“bar”写进我的文本文件,而是提醒“42”?
NB: Earlier revisions of this question were explicitly about PHP on the server and JavaScript on the client. The essential nature of the problem and solutions is the same for any pair of languages when one is running on the client and the other on the server. Please take this in to account when you see answers talking about specific languages.
NB:之前对这个问题的修改是关于服务器上的PHP和客户机上的JavaScript。当一种语言在客户机上运行,另一种语言在服务器上运行时,问题和解决方案的本质是相同的。当你看到有关特定语言的答案时,请把这个考虑进去。
4 个解决方案
#1
373
Your code is split into two entirely separate parts, the server side and the client side.
您的代码被分为两个完全独立的部分,服务器端和客户端。
|
---------->
HTTP request
|
+--------------+ | +--------------+
| | | | |
| browser | | | web server |
| (JavaScript) | | | (PHP etc.) |
| | | | |
+--------------+ | +--------------+
|
client side | server side
|
<----------
HTML, CSS, JavaScript
|
The two sides communicate via HTTP requests and responses. PHP is executed on the server and outputs some HTML and maybe JavaScript code which is sent as response to the client where the HTML is interpreted and the JavaScript is executed. Once PHP has finished outputting the response, the script ends and nothing will happen on the server until a new HTTP request comes in.
双方通过HTTP请求和响应进行通信。PHP在服务器上执行,并输出一些HTML或JavaScript代码,这些代码作为响应发送到客户端,在那里解释HTML并执行JavaScript。一旦PHP完成输出响应,脚本就结束了,在新的HTTP请求进入之前服务器上不会发生任何事情。
The example code executes like this:
示例代码如下所示:
<script type="text/javascript">
var foo = 'bar';
<?php
file_put_contents('foo.txt', ' + foo + ');
?>
var baz = <?php echo 42; ?>;
alert(baz);
</script>
Step 1, PHP executes all code between <?php ?>
tags. The result is this:
步骤1,PHP执行 标记。结果是这样的:
<script type="text/javascript">
var foo = 'bar';
var baz = 42;
alert(baz);
</script>
The file_put_contents
call did not result in anything, it just wrote " + foo + " into a file. The <?php echo 42; ?>
call resulted in the output "42", which is now in the spot where that code used to be.
file_put_contents调用没有产生任何结果,它只是将“+ foo +”写入文件。< ?php echo 42;?>调用的结果是输出“42”,它现在位于代码以前所在的位置。
This resulting HTML/JavaScript code is now sent to the client, where it gets evaluated. The alert
call works, while the foo
variable is not used anywhere.
生成的HTML/JavaScript代码现在被发送到客户机,在那里对其进行评估。警报调用工作,而foo变量不在任何地方使用。
All PHP code is executed on the server before the client even starts executing any of the JavaScript. There's no PHP code left in the response that JavaScript could interact with.
在客户机开始执行任何JavaScript之前,所有PHP代码都在服务器上执行。在JavaScript可以与之交互的响应中没有留下PHP代码。
To call some PHP code, the client will have to send a new HTTP request to the server. This can happen using one of three possible methods:
要调用一些PHP代码,客户端必须向服务器发送一个新的HTTP请求。这可以通过以下三种方法之一实现:
- A link, which causes the browser to load a new page.
- 一个链接,导致浏览器加载一个新页面。
- A form submission, which submits data to the server and loads a new page.
- 表单提交,它向服务器提交数据并加载新页面。
- An AJAX request, which is a Javascript technique to make a regular HTTP request to the server (like 1. and 2. will), but without leaving the current page.
- AJAX请求,这是向服务器发出常规HTTP请求的Javascript技术(如1)。和2。将),但不离开当前页。
Here's a question outlining these method in greater detail
下面是一个更详细地概述这些方法的问题
You can also use JavaScript to make the browser open a new page using window.location
or submit a form, emulating possibilities 1. and 2.
您还可以使用JavaScript使浏览器使用窗口打开一个新页面。位置或提交表单,模拟可能性1。和2。
#2
130
To determine why PHP code doesn't work in JavaScript code we need to understand what is client side and server side language and how they work.
要确定为什么PHP代码不能在JavaScript代码中工作,我们需要了解什么是客户端和服务器端语言以及它们是如何工作的。
Server-side languages (PHP etc.): They retrieve records from databases, maintain state over the stateless HTTP connection, and do a lot of things that require security. They reside on the server, these programs never have their source code exposed to the user
服务器端语言(PHP等):它们从数据库检索记录,在无状态HTTP连接上维护状态,并做很多需要安全性的事情。它们驻留在服务器上,这些程序的源代码永远不会公开给用户
图像attr
So you can easily see that server side language handle HTTP request and process it and as @deceze said PHP is executed on the server and outputs some HTML and maybe JavaScript code which is sent as response to the client where the HTML is interpreted and the JavaScript is executed.
所以你可以很容易地看到,服务器端语言处理HTTP请求和处理它和@deceze说PHP服务器上执行和输出一些HTML和JavaScript代码可能发送响应给客户端,解释HTML和JavaScript执行。
While at the other hand Client Side Language (like JavaScript) resides on browser and runs at the browser, Client-side scripting generally refers to the class of computer programs on the web that are executed client-side, by the user's web browser, instead of server-side.
而另一方面,客户端语言(如JavaScript)驻留在浏览器上,在浏览器上运行,客户端脚本通常指的是web上的计算机程序类,这些程序是由用户的web浏览器而不是服务器端执行的。
JavaScript is visible to the user and can be easily modified so for security stuff we must not rely on JavaScript.
JavaScript对用户是可见的,并且可以很容易地修改,因此对于安全内容,我们不能依赖于JavaScript。
So when you make a HTTP request on server than The server first reads the PHP file carefully to see if there are any tasks that need to be executed and send response to client side and again as @deceze said *Once PHP has finished outputting the response, the script ends and nothing will happen on the server until a new HTTP request comes in.*
当你做一个HTTP请求首先读取服务器的服务器上的PHP文件仔细看看是否有任何需要执行的任务,并将响应发送到客户端,再次@deceze说*一旦PHP完成输出响应,服务器上的脚本结束,什么都不会发生,直到一个新的HTTP请求。*
图片来源
So now what can I do if I need to call PHP? It depends how you need to do it: either by reloading the page or by using an AJAX call.
如果我需要调用PHP,我能做什么呢?这取决于您需要如何做:通过重载页面或使用AJAX调用。
- You can do by reloading page and send HTTP request
- 可以通过重载页面并发送HTTP请求来实现
- you can make AJAX call with JavaScript and this does not require reloading page
- 您可以使用JavaScript进行AJAX调用,这并不需要重新加载页面
Good Read:
好读:
- Wikipedia : Server-side scripting
- *:服务器端脚本
- Wikipedia : Client-side scripting
- *:客户端脚本
- Madara Uchiha : Difference between client side and server side programming
- Madara Uchiha:客户端和服务器端编程的区别
#3
19
Your Javascript will execute on the client, not on the server. This means that foo
is not evaluated on the server side and therefore its value can't be written to a file on the server.
Javascript将在客户机上执行,而不是服务器上。这意味着foo不是在服务器端进行评估的,因此它的值不能写入服务器上的文件。
The best way to think about this process is as if you're generating a text file dynamically. The text you're generating only becomes executable code once the browser interprets it. Only what you place between <?php
tags is evaluated on the server.
考虑这个过程的最好方法是动态生成一个文本文件。您正在生成的文本只有在浏览器对其进行解释后才能成为可执行代码。只有你在
By the way, making a habit of embedding random pieces of PHP logic in HTML or Javascript can lead to seriously convoluted code. I speak from painful experience.
顺便说一句,养成在HTML或Javascript中嵌入任意PHP逻辑片段的习惯会导致非常复杂的代码。我从痛苦的经历说起。
#4
2
In web application every task execute in a manner of request and response.
在web应用程序中,每个任务都以请求和响应的方式执行。
Client side programming is with html code with Java script and its frameworks, libraries executes in the internet explorer, Mozilla, chrome browsers. In the java scenario server side programming servlets executes in the Tomcat, web-logic , j boss, WebSphere severs
客户端编程使用html代码和Java脚本及其框架,库在internet explorer、Mozilla、chrome浏览器中执行。在java场景中,服务器端编程在Tomcat、web逻辑、j boss、WebSphere severs中执行
#1
373
Your code is split into two entirely separate parts, the server side and the client side.
您的代码被分为两个完全独立的部分,服务器端和客户端。
|
---------->
HTTP request
|
+--------------+ | +--------------+
| | | | |
| browser | | | web server |
| (JavaScript) | | | (PHP etc.) |
| | | | |
+--------------+ | +--------------+
|
client side | server side
|
<----------
HTML, CSS, JavaScript
|
The two sides communicate via HTTP requests and responses. PHP is executed on the server and outputs some HTML and maybe JavaScript code which is sent as response to the client where the HTML is interpreted and the JavaScript is executed. Once PHP has finished outputting the response, the script ends and nothing will happen on the server until a new HTTP request comes in.
双方通过HTTP请求和响应进行通信。PHP在服务器上执行,并输出一些HTML或JavaScript代码,这些代码作为响应发送到客户端,在那里解释HTML并执行JavaScript。一旦PHP完成输出响应,脚本就结束了,在新的HTTP请求进入之前服务器上不会发生任何事情。
The example code executes like this:
示例代码如下所示:
<script type="text/javascript">
var foo = 'bar';
<?php
file_put_contents('foo.txt', ' + foo + ');
?>
var baz = <?php echo 42; ?>;
alert(baz);
</script>
Step 1, PHP executes all code between <?php ?>
tags. The result is this:
步骤1,PHP执行 标记。结果是这样的:
<script type="text/javascript">
var foo = 'bar';
var baz = 42;
alert(baz);
</script>
The file_put_contents
call did not result in anything, it just wrote " + foo + " into a file. The <?php echo 42; ?>
call resulted in the output "42", which is now in the spot where that code used to be.
file_put_contents调用没有产生任何结果,它只是将“+ foo +”写入文件。< ?php echo 42;?>调用的结果是输出“42”,它现在位于代码以前所在的位置。
This resulting HTML/JavaScript code is now sent to the client, where it gets evaluated. The alert
call works, while the foo
variable is not used anywhere.
生成的HTML/JavaScript代码现在被发送到客户机,在那里对其进行评估。警报调用工作,而foo变量不在任何地方使用。
All PHP code is executed on the server before the client even starts executing any of the JavaScript. There's no PHP code left in the response that JavaScript could interact with.
在客户机开始执行任何JavaScript之前,所有PHP代码都在服务器上执行。在JavaScript可以与之交互的响应中没有留下PHP代码。
To call some PHP code, the client will have to send a new HTTP request to the server. This can happen using one of three possible methods:
要调用一些PHP代码,客户端必须向服务器发送一个新的HTTP请求。这可以通过以下三种方法之一实现:
- A link, which causes the browser to load a new page.
- 一个链接,导致浏览器加载一个新页面。
- A form submission, which submits data to the server and loads a new page.
- 表单提交,它向服务器提交数据并加载新页面。
- An AJAX request, which is a Javascript technique to make a regular HTTP request to the server (like 1. and 2. will), but without leaving the current page.
- AJAX请求,这是向服务器发出常规HTTP请求的Javascript技术(如1)。和2。将),但不离开当前页。
Here's a question outlining these method in greater detail
下面是一个更详细地概述这些方法的问题
You can also use JavaScript to make the browser open a new page using window.location
or submit a form, emulating possibilities 1. and 2.
您还可以使用JavaScript使浏览器使用窗口打开一个新页面。位置或提交表单,模拟可能性1。和2。
#2
130
To determine why PHP code doesn't work in JavaScript code we need to understand what is client side and server side language and how they work.
要确定为什么PHP代码不能在JavaScript代码中工作,我们需要了解什么是客户端和服务器端语言以及它们是如何工作的。
Server-side languages (PHP etc.): They retrieve records from databases, maintain state over the stateless HTTP connection, and do a lot of things that require security. They reside on the server, these programs never have their source code exposed to the user
服务器端语言(PHP等):它们从数据库检索记录,在无状态HTTP连接上维护状态,并做很多需要安全性的事情。它们驻留在服务器上,这些程序的源代码永远不会公开给用户
图像attr
So you can easily see that server side language handle HTTP request and process it and as @deceze said PHP is executed on the server and outputs some HTML and maybe JavaScript code which is sent as response to the client where the HTML is interpreted and the JavaScript is executed.
所以你可以很容易地看到,服务器端语言处理HTTP请求和处理它和@deceze说PHP服务器上执行和输出一些HTML和JavaScript代码可能发送响应给客户端,解释HTML和JavaScript执行。
While at the other hand Client Side Language (like JavaScript) resides on browser and runs at the browser, Client-side scripting generally refers to the class of computer programs on the web that are executed client-side, by the user's web browser, instead of server-side.
而另一方面,客户端语言(如JavaScript)驻留在浏览器上,在浏览器上运行,客户端脚本通常指的是web上的计算机程序类,这些程序是由用户的web浏览器而不是服务器端执行的。
JavaScript is visible to the user and can be easily modified so for security stuff we must not rely on JavaScript.
JavaScript对用户是可见的,并且可以很容易地修改,因此对于安全内容,我们不能依赖于JavaScript。
So when you make a HTTP request on server than The server first reads the PHP file carefully to see if there are any tasks that need to be executed and send response to client side and again as @deceze said *Once PHP has finished outputting the response, the script ends and nothing will happen on the server until a new HTTP request comes in.*
当你做一个HTTP请求首先读取服务器的服务器上的PHP文件仔细看看是否有任何需要执行的任务,并将响应发送到客户端,再次@deceze说*一旦PHP完成输出响应,服务器上的脚本结束,什么都不会发生,直到一个新的HTTP请求。*
图片来源
So now what can I do if I need to call PHP? It depends how you need to do it: either by reloading the page or by using an AJAX call.
如果我需要调用PHP,我能做什么呢?这取决于您需要如何做:通过重载页面或使用AJAX调用。
- You can do by reloading page and send HTTP request
- 可以通过重载页面并发送HTTP请求来实现
- you can make AJAX call with JavaScript and this does not require reloading page
- 您可以使用JavaScript进行AJAX调用,这并不需要重新加载页面
Good Read:
好读:
- Wikipedia : Server-side scripting
- *:服务器端脚本
- Wikipedia : Client-side scripting
- *:客户端脚本
- Madara Uchiha : Difference between client side and server side programming
- Madara Uchiha:客户端和服务器端编程的区别
#3
19
Your Javascript will execute on the client, not on the server. This means that foo
is not evaluated on the server side and therefore its value can't be written to a file on the server.
Javascript将在客户机上执行,而不是服务器上。这意味着foo不是在服务器端进行评估的,因此它的值不能写入服务器上的文件。
The best way to think about this process is as if you're generating a text file dynamically. The text you're generating only becomes executable code once the browser interprets it. Only what you place between <?php
tags is evaluated on the server.
考虑这个过程的最好方法是动态生成一个文本文件。您正在生成的文本只有在浏览器对其进行解释后才能成为可执行代码。只有你在
By the way, making a habit of embedding random pieces of PHP logic in HTML or Javascript can lead to seriously convoluted code. I speak from painful experience.
顺便说一句,养成在HTML或Javascript中嵌入任意PHP逻辑片段的习惯会导致非常复杂的代码。我从痛苦的经历说起。
#4
2
In web application every task execute in a manner of request and response.
在web应用程序中,每个任务都以请求和响应的方式执行。
Client side programming is with html code with Java script and its frameworks, libraries executes in the internet explorer, Mozilla, chrome browsers. In the java scenario server side programming servlets executes in the Tomcat, web-logic , j boss, WebSphere severs
客户端编程使用html代码和Java脚本及其框架,库在internet explorer、Mozilla、chrome浏览器中执行。在java场景中,服务器端编程在Tomcat、web逻辑、j boss、WebSphere severs中执行