最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why can't server side and client side scripts interact? - Stack Overflow

programmeradmin2浏览0评论

I'm new to client and server side scripts, and I was wondering, how e they can't interact?

The major difference Code Conquest stated here is that:

... is called a client side language is because it runs scripts on your puter after you’ve loaded a web page.

And

A server side or back-end language runs its scripts before the HTML is loaded, not after.

Even if the server side script (e.g. PHP) has already been executed, how e it can't be changed (with JavaScript) after the page has loaded?

I want to call PHP from JS. For example, is there a possible way to do this?

setInterval(<?php someFunction() ?>,10000);

Sorry if I'm misinterpreting something, and please point it out to me.

I'm new to client and server side scripts, and I was wondering, how e they can't interact?

The major difference Code Conquest stated here is that:

... is called a client side language is because it runs scripts on your puter after you’ve loaded a web page.

And

A server side or back-end language runs its scripts before the HTML is loaded, not after.

Even if the server side script (e.g. PHP) has already been executed, how e it can't be changed (with JavaScript) after the page has loaded?

I want to call PHP from JS. For example, is there a possible way to do this?

setInterval(<?php someFunction() ?>,10000);

Sorry if I'm misinterpreting something, and please point it out to me.

Share Improve this question asked Oct 31, 2013 at 22:28 Jonathan LamJonathan Lam 17.4k17 gold badges71 silver badges99 bronze badges 2
  • look at JSON RPC json-rpc – gawpertron Commented Oct 31, 2013 at 22:30
  • 2 This is what happens when your browser loads a website. Request to server for HTML file => PHP runs and generates an HTML file => PHP quits => browser receives that file and renders it and runs any JavaScript. When your browser gets the page, PHP is done, and the connection to the server is closed. You need to look into AJAX to do what you want. – gen_Eric Commented Oct 31, 2013 at 22:30
Add a ment  | 

5 Answers 5

Reset to default 3

Well they can interact, but not like you are imagining it.

Think of client side JavaScript as the browser, and think of server side code, well as the server.

Then they would municate by sending messages between each other, the most monly used method for exchanging messages would be JSON.

To sum this up, client side code can municate with the server be sending messages through GET or POST requests, or by using AJAX.

The server can respond to those messages, and it can also (this was added in HTML 5 standard) send events to the client using WebSockets.

They can't interact (sort of - explained in more detail below) because they run on two different places. Server Side Scripts - eg, PHP, will run on the server and plete running before data is sent to the user's PC.

Client Side scripts (eg, Javascript) START running when the user finishes loading the page data.

So, in short, the PHP finishes executing before the JS even starts to execute - hence they can't really interact.

How you make them interact is through some trickery and calls inside the JS to request aadditional data from the server - so at that point they will still run individually, but you can get them to talk via additional calls made in the page.

Using this approach, even though they are being executed totally independantly of one another, they are able to exchange information via data (such as JSON objects) and give the impression that they are working together, although they are doing it in totally different places.

Non Technical Analogy

To use a pletely non IT analogy which will hopefully make it clearer, lets say that server side scripts are an army base. Client Side scripts are units deployed into another country. They are all there to acplish a particular task, but they work totally independantly of one another. Now, JSON is like a phone call between them. The deployed units can make a call to the base, ask for further instructions and then execute them, but as far as the base is concerned, they have no idea what is happening until the deployed units call home again - so they can SORT-OF work together, but do so individually and without knowing what the other is really doing.

The easiest method for this is to use AJAX, it's a lightweight way of dynamically generating/finding content.

var ajax_call = function() {
    $.ajax({ url: 'script.php?argument=value' }); // gets the php code you need.
};

var interval = 1000 * 60 * X; // X = amount of minutes it will take till it executes.

setInterval(ajax_call, interval); // sets the ajax_call function in motion

Let me know if this works.

Simple example:

var request = new XMLHttpRequest();
request.open('GET', 'http://example./yourfile.php?func=someFunc', false);
request.send();

setInterval(request.responseText, 10000);

Or assign to var:

yourvar = request.responseText;
setInterval(yourvar, 10000);

Then in yourfile.php:

$func = $_GET['func'];
//use $func to call func or do other stuff

Client side runs on the puter browser. Server side runs on the server and generates the code that is sent to the client.

Ajax or jQuery can fetch and send data directly from the server.

发布评论

评论列表(0)

  1. 暂无评论