Balancing AJAX and PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RenX99
Forum Newbie
Posts: 1
Joined: Wed Dec 29, 2010 10:29 am

Balancing AJAX and PHP

Post by RenX99 »

I'm new to this board, so if this has been addressed before, sorry, I couldn't find anything in the searches when I looked.

I consider myself an intermediate programmer, not a professional, but I do know more than most 'newbies' but am by no means an expert. I've created sites using various techniques, using just php, templates, and now doing a more "ajax" type of site using a lot of jquery and straight javascript. My question to you is, what type of guidelines do you use when you design your sites? How much of your site's abilities do you build with php and how much with javascript. For example, on the site i'm currently working all I'm doing with php, is generating JSON objects that I then work with that data on the client side. As my site gets more and more complex, I'm starting to wonder if I should have just used more php and less javascript. I'm trying to get an idea, as to where to draw the line of what falls into what side of the equation.

I supposed my issue maybe that I'm just not putting php and jquery together in a graceful way.. I am primarily self taught, learning from my mistakes as I go... a lot of mistakes. :? So I guess what I really want to know is, how do you decide what parts of a website really need to be AJAX and what doesn't? I'm not even sure if that question makes sense, maybe I'm over thinking this.

Thanks in advance,

Rene
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Balancing AJAX and PHP

Post by McInfo »

In general and especially for transactional sites, think of JavaScript as a way to add convenience and entertainment. If the site is unable to complete its critical duties without JavaScript, it relies too heavily on JavaScript.

For some highly-dynamic sites, JavaScript is necessary to provide adequate response time or to relieve the burden on the server. Some creative things are just not possible or practical without JavaScript.

Decide what the critical components of your system are. Implement them with traditional methods, if possible. If it adds convenience to your user, overlay JavaScript functionality. The redundancy allows the application to gracefully fall back to the lower layer if the user has JavaScript disabled.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Balancing AJAX and PHP

Post by califdon »

I agree fully with McInfo, but thought I would add another perspective on the choices you must make.

As you well know, PHP is server-side, JS is browser-side, AJAX combines the two. Generally, anything that you can accomplish on the server before sending a page to the browser will relieve the browser from the need to perform the same tasks, so it probably will result in faster page loading. But some operations depend on user interaction, so they must be accomplished with JS, after the page is loaded in the browser. And if some of those tasks require additional data from the server, you will have to use AJAX or reload a new page. If it is practical to supply all data from the server in the initial page load, you can avoid the second request to the server, but if the amount of data is very large, this becomes impractical. And, of course, you need to consider what the impact will be if the browser has disabled JS, which can be the case in perhaps 5% of browsers, according to estimates I've seen.

My personal thought process goes something like this: do as much as is possible in PHP on the server, use as little JS as I can, unless it won't be a problem in the case of JS disabled browsers, use AJAX where it will enhance the user experience.
Post Reply