开发者

javascript vs. php : pros and cons for code development

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 in开发者_Go百科to 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?


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)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜