AJAX and how to best deal with it server side in PHP AJAX and how to best deal with it server side in PHP codeigniter codeigniter

AJAX and how to best deal with it server side in PHP


I recently completed an AJAX heavy site using CodeIgniter and learned a lot in the process. These are some things I learned (read: mistakes I made):

Don't return html pages and json from the same method.

At first it seemed like a good idea to have a set up like this:

/controller/method/format

where the default for format was 'html'. The idea was, I could have something like /object/view show the html page for that object and have something like /object/view/json return the json data for that object.

In practice this makes things very messy. You end up mixing a lot of different functionality in a single method which makes the code that much more complicated. Ick.

Instead, what I do now is add an 'api' subfolder under application/controllers. The controllers in that subfolder only return json data. It's much cleaner.

Don't reimplement your templates in javascript

There are two approaches when fetching stuff via an AJAX call. You can return some sort of encoded data (JSON, XML) and insert that into the correct spot on the page. Or you can render a chunk of html and then insert that into a div somewhere.

If you go the data route try to stick to very simple markup structure like a span or a simple unordered list, for example. In my case, I had a certain part of the navigation that was returned from the server as an array of JSON encoded links.

My javascript would then spit them out in the correct structure for the menu. When the menu became more complicated my script incorporated this complexity. Now the markup for that menu is duplicated in the CI templates as well as the javascript.

In this case it would have been better to have the server return a chunk of html containing the menu markup instead of trying to build it from the data on the client.

Use AJAX sparingly/carefully/only as needed

You can save some server resources by using AJAX requests smartly. However, if you get carried away (*coughlikemecough*) you quickly can find yourself running several AJAX requests per user action. This is typically worse from a performance perspective as each request has its share of overhead and that overhead will negate an benefit from only requesting the needed resources.

If you find that clicking an interface element triggers more than one AJAX request that's a red flag to think hard about whats going on. You may be creating complexity without benefit.

JSON encoding is nice

You can json_encode() the results of a database query and it will generate really nice objects that you can work with in your script. The syntax is clean and works well with the results that CodeIgniter's db functions return.

Hope you find some of that useful. Always better when you can learn from someone else's mistakes! :)


JSON / XML or plain

I think you have to make a choice yourself. You could use XML or JSON or just plain html. It also depends on the complexity of your problem. I would choice json because PHP5 has really simple methods to encode and decode json.

Server-side

  • Just put AJAX actions in a controller. Use get for getting data and codeigniter post function to store data on server.
  • I would advice to have a look at yahoo's developer best practices which will ensure your site will run lightning fast. Also ajax request should be cached if possible.

Javascript

jquery makes json calls really simple. You could use jQuery.get to retrieve information and Jquery.post to store data.

Example

I will also try to put an simple example online for you to look at.

Good luck,
Alfred Westerveld


well, it depends on what exactly want to do. because the simple way to use ajax functions - use jQuery framework. It has everything you need.