What is the difference between client-side and server-side programming?
Asked Answered
L

3

572

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.

Libradalibrarian answered 12/12, 2012 at 13:3 Comment(0)
L
531

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:

  1. A link, which causes the browser to load a new page.
  2. A form submission, which submits data to the server and loads a new page.
  3. 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.

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.

Libradalibrarian answered 12/12, 2012 at 13:3 Comment(6)
You can also open second page using window.open or load a page using an iframe.Abernon
It might be worth adding WebSockets to the list of communication methods.Monkery
What if let's say drop down values get updated via jquery. When user does step 2. A form submission, which submits data to the server and loads a new page, via "Submit" button would the jquery updated values be able to be passed to a controller in php? Or would they not be visible to php since it was added to the dom via jquery? @LibradalibrarianEmbay
@Fabricio An HTTP request will be created from the <form> data and sent to the server. You can manipulate forms using Javascript to have them contain different data. Yes, that data will be part of the resulting HTTP request if it's properly part of the form when it is being submitted; it doesn't matter whether it was in the original HTML or added afterwards via Javascript.Libradalibrarian
You say it just wrote " + foo + " into a file, so presumably, that file now resides on the server's filesystem somewhere? It's a little unclear because it is just not explicitly stated in your answer.Holmquist
Should be noted, we have not just PHP, meanwhile we also have Node.js which is JavaScript running on the server side. So, you can run JavaScript on the client side as well as on the server side. However, the environment is completely different. For example, on the client side you can access the window object (which is the current HTML page) but for security reason you cannot access the local filesystem directly. On the server side it is the opposite.Brownnose
C
189

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 from wikipedia_http://en.wikipedia.org/wiki/File:Scheme_dynamic_page_en.svg 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.*

Graphical representation

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.

  1. You can do so by reloading the page and sending a HTTP request
  2. You can make an AJAX call with JavaScript - this does not require reloading page

Good Read:

  1. Wikipedia : Server-side scripting
  2. Wikipedia : Client-side scripting
  3. Madara Uchiha : Difference between client side and server side programming
Choreography answered 1/7, 2013 at 11:52 Comment(0)
J
38

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.

Jordanna answered 12/12, 2012 at 13:46 Comment(1)
Your answer here is noteworthy as it makes mention to the/a interpeter. However, javascript can be compiled and run in a server environment, and it can be interpeted by a server as well.Wart

© 2022 - 2024 — McMap. All rights reserved.