2

If a user refreshes a page I need to send the data using php as it accesses a mysql table.

If the user adds content, I don't want to run an AJAX call "first" as I can simply and immediately update the DOM, and then send a one-way ajax call to store it in the mysql table.

So on a referesh I have PHP creating my XHTML and sending it to the Browser.

On user input, I have the DOM update immediately followed by ajax call to put it in the mysql table.

Thing is I have to write code in JS and PHP for each user action that modifies the page.

Should I have the data sent to the Javascript for entry into the DOM and not do less with it in the PHP. What are the tradeoffs from taking user input and converting it to the UI with javascript vs. php?

Should I offload as much as possible to the client to reduce server load?

2
  • 1
    Always consider graceful degradation in the absence of client-side scripting. That basically answers it... Commented Aug 29, 2011 at 0:16
  • People like you are the reason I drink. Commented Aug 31, 2011 at 0:40

3 Answers 3

1

You have answered it yourself:

  • With php you need to send it through ajax and wait for response
  • With javascript you need to maintain 2 set of templates (server- and client-side one)
Sign up to request clarification or add additional context in comments.

12 Comments

well, say on a refresh, i have mysql data I need inserted into xhtml and displayed to the user, if i do this on the php side, it takes up server processor, if I just send the data to the client and have javascript create the xhtml/dom, i use up client processor...
to refine my quesion, which wasy is "best practice"
@Chris Aaker: composing some html is not that expensive. You need to send data to server anyway, to persist it. And persisting consumes much more than template transformation. So the answer comes to something like: if you need responsive UI - perform it on client, if you need your client to know if the data has been successfully saved - do that on server.
"Best practice" is whatever does it best for you. You are dealing with a standard client/server architecture decision. In most cases, the client is providing a UI, the server is providing the business logic and data processing. Relying on them to process data independently and remain consistent is not a good idea: send user input to the server for processing, get updated data from the server, then present it at the client.
For my purposes, I want immediate updating of the UI so I am putting as much as possible in to the Javascript, after I have updated the UI I send an AJAX request to update the Server with persistent data. On a page reload likewise I have the client build out as much as possible via DOM access as opposed to having PHP create my .html content. Considering equipment costs, offloading to clients is smart in this regards...seems there are a few good reasons for client side development..., server side - persistent and consolidated data
|
1

If you need to do something with the data serverside (validation, processing, etc), you can either use JavaScript with AJAX, or send it off on a page reload using POST or GET, depending on what you're sending. If you don't need to do anything with the data serverside, then using JavaScript to modify the DOM immediately is fine.

Comments

0

DOM operations aren't fast. Try it and you'll see that is better for your users to pass real HTML as it is or JSON-embedded with AJAX requests. Even big names like Twitter do it so.

If you still want to get away without PHP consider server-based JavaScript, e.g. Node.js.

5 Comments

DOM access will always be faster then AJAX unless you are defining them different from me.
@sanmai: twitter uses ajax, yes, but it returns json raw data using its own REST api. Thus composes the content on the client from raw data (as OP wants)
@zerkms Twitter returns json embedded html for some parts of the data. It is easy to see.
@sanmai: curious of any sample. But even if there is some - it is not a rule, but an exception.
@zerkms you're wrong. Try walking around Facebook with Firebug network console switched on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.