View Full Version : ajax: js vs php

01-12-2007, 03:05 PM
Hey there,

newbie as I am(php,mysql and hopefully ajax in a couple of month), I have to figure out some issues but I 'm not really sure if this is the place to do so..Anyway..

I ' ve read that javascript offers the ability to get content on the fly (and that 's why I want to get myself into ajax). Plus, it 's widely known that js is client side while php server side. So the question goes like that: Is this on-the-fly feauture supported by php and if no, why? If, for example, somebody wants to write a script to add tab support to his website, what 's the reason to choose js over php?

The answer may be obvious for some of you guys anad if it really is, feel free to reply =) Thnx in advance

01-12-2007, 03:42 PM
Simply: JavaScript is the only processing language (the only one worth mentioning, at least) on the client side. EVERYTHING you want to do on the client has to be written in JavaScript. You can't get around this. If you want to react on user input, or do on-the-fly changes in the user side content or interface, JavaScript is your only option. PHP simply can't do what JavaScript can do.

PHP is server side. The only way to get things from client to server or from server to client is through HTTP requests. In the browser, traditionally HTTP requests to the server are results of clicking links or submitting forms. The response replaces the entire current page in those cases.

So, PHP alone can only do this page-by-page style of interactions because HTML+CSS without JavaScript can only do things in this way. However, JavaScript can as I mentioned react on user input and manipulate user content or interface. JavaScript can also send HTTP requests to the server through either the traditional model; or by creating new script elements or by using XMLHttpRequest. To PHP, these requests are just like any other, traditional requests. The difference is that a new script element added to an HTML document will not reload the page but instead be executed within the page; and an XMLHttpRequest gives the JavaScript in the page ability to do whatever it want with the data sent from the server.

So, it's not a question of JavaScript versus PHP. JavaScript can interact with the user, while PHP cannot. PHP on the other hand sits on the server and can use databases and files on the server, something JavaScript cannot. JavaScript and PHP cooperating can connect a user action, through JavaScript making a request, to a PHP script handling a server resource, through PHP sending a response, to some JavaScript code communicating to the user whatever contents the response contained, or if not data for the user, then it can handle the data as is needed.

Note here that all in all, for PHP there is no real change. PHP is still just reacting to requests and generating responses, essentially it's purpose is string handling and database interactions. The difference is that JavaScript allows a different client side handling requests - the request doesn't have to replace the current page on the client any longer, it can just add data, behaviour or state to it.

01-12-2007, 08:22 PM
Aha.. simple as that.. very helpful reply. thnx =)

01-13-2007, 09:10 AM
PHP is... Hypertext PREprocessor emphasis on PRE. I wish there was a way to use php on the clientside as well because I know it better... But oh well.. I'm on to Javascript next

01-13-2007, 09:40 AM
php (or any other server-language) is not oriented on document's elements. It can not "catch" the user's actions.

Javascript has it's own Object Oriented Analysis. Javascript works like that:

A document has elements (tags, text, comments...). Elements have attributes. Attributes have values. Javascript is able to identify/create/change/remove the elements/attributes/values. This is the main aim of this language.To do that, javascript uses methods, nested in functions, ruled by a personal syntax. Functions (or at least one function, the start function) are triggered by events, following an user's action. Functions can be called one from inside each other. Functions and methods can carry arguments.

Javascript is an Object Oriented Language, but not a class oriented (at least the common versions are not, so far), it is a prototype oriented language. That means javascript is able to handle everything as Objects, and can create/set/group/sort/ungroup/ them.

Javascript can make Objects interact one with each other, and it can assign new custom properties to them (along with the possibility, which I have mentioned before, to set/change/remove the native properties of the Objects, if any).

There are few "native" Objects in Javascript (for instance Date() object...). Most of the time Objects must be created (along with some custom properties) upon needs. This is to to be made on using functions, native Objects (or with JSON), or prototypes.

This "dual" Object Analysis (element with its attributes / object with its properties) is the key of the javascript understanding.