Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: ajax: js vs php

  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ajax: js vs php

    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

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    Last edited by liorean; 01-12-2007 at 03:44 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aha.. simple as that.. very helpful reply. thnx =)

  • #4
    New Coder
    Join Date
    Jan 2007
    Posts
    64
    Thanks
    0
    Thanked 1 Time in 1 Post
    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
    No trees were harmed in the transmission of this message.
    However, a rather large number of electrons were temporarily
    inconvenienced.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    Last edited by Kor; 01-13-2007 at 09:54 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •