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"?
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 (even if they are the same language). Please take this in to account when you see answers talking about specific languages.
Server-side scripting is used at the backend, where the source code is not viewable or hidden at the client side (browser). On the other hand, client-side scripting is used at the front end which users can see from the browser. When a server-side script is processed it communicates to the server.
It helps provide a response to every request that comes in from the user/client. This is not visible to the client side of the application. It requires the interaction with the server for the data to be process. Server side scripting requires languages such as PHP, ASP.net, ColdFusion, Python, Ruby on Rails.
A server is a sample of software or hardware that serves a specific service to its clients. Web servers, domain name servers, and mail servers are some of the example servers using by all network users. A client is a user program that connects to a server to access a service.
The client-side of a website refers to the web browser and the server-side is where the data and source code is stored.
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.
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:
<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.
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.
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.
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:
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.
To determine why PHP code doesn't work in JavaScript code we need to understand what client side and server side languages are, and how they work.
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.
image attr
So you can easily see that server side languages handle HTTP requests and process them, and, as @deceze said, PHP is executed on the server and outputs some HTML, and maybe JavaScript code, which is sent as a response to the client, where the HTML is interpreted and JavaScript is executed.
On the other hand, Client Side Languages (like JavaScript) reside in browser and run in 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 is visible to the user and can be easily modified, so for security stuff we must not rely on JavaScript.
So when you make a HTTP request on server, the server first reads the PHP file carefully to see if there are any tasks that need to be executed, and sends a response to the client side. 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.*
Image source
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.
Good Read:
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.
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.
In web application every task execute in a manner of request and response.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With