javascript - What is the difference between client-side and server-side programming? -




i have code:

<script type="text/javascript">     var foo = 'bar';     <?php         file_put_contents('foo.txt', ' + foo + ');     ?>      var baz = <?php echo 42; ?>;     alert(baz); </script> 

why not write "bar" text file, alerts "42"?


nb: earlier revisions of question explicitly php on server , javascript on client. essential nature of problem , solutions same any pair of languages when 1 running on client , other on server. please take in account when see answers talking specific languages.

your code split 2 entirely separate parts, server side , client side.

                    |                ---------->               http request                     | +--------------+    |    +--------------+ |              |    |    |              | |    browser   |    |    |  web  server | | (javascript) |    |    |  (php etc.)  | |              |    |    |              | +--------------+    |    +--------------+                     |   client side       |      server side                     |                <----------           html, css, javascript                     | 

the 2 sides communicate via http requests , responses. php executed on server , outputs html , maybe javascript code sent response client html interpreted , javascript executed. once php has finished outputting response, script ends , nothing happen on server until new http request comes in.

the example code executes 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 code between <?php ?> tags. result this:

<script type="text/javascript">     var foo = 'bar';      var baz = 42;     alert(baz); </script> 

the file_put_contents call did not result in anything, wrote " + foo + " file. <?php echo 42; ?> call resulted in output "42", in spot code used be.

this resulting html/javascript code sent client, gets evaluated. alert call works, while foo variable not used anywhere.

all php code executed on server before client starts executing of javascript. there's no php code left in response javascript interact with.

to call php code, client have send new http request server. can happen using 1 of 3 possible methods:

  1. a link, causes browser load new page.
  2. a form submission, submits data server , loads new page.
  3. an ajax request, javascript technique make regular http request server (like 1. , 2. will), without leaving current page.

here's question outlining these method in greater detail

you can use javascript make browser open new page using window.location or submit form, emulating possibilities 1. , 2.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -