Creating an Ajax-Enabled Rating System for Your Website

Allowing for visitors to rate products and content on your website will improve the usefulness of your site and provide you with valuable feedback. An Ajax-enabled rating system is not hard to implement and, more importantly, is easy for your visitors to use. In this article, Larry Ullman walks you through the steps for adding an Ajax-enabled rating system to your site.

From the author of

From the author of

Whether your site sells products or provides content, having a rating system in place, where visitors can quickly rank what they bought or read, is tremendously useful and takes little time or effort to implement. Ratings can help customers make purchasing decisions and help site administrators know what products or articles are worth promoting and which ones ought to be cast aside. The success of such systems depends upon visitors actually using it, which means it must be simple and reliable. Ajax, when done right, is therefore the perfect tool for the job.

NOTE

The ideas in this article can also be used to expand the two example e-commerce sites in Larry Ullman’s book Effortless E-Commerce with PHP and MySQL. You can learn more about that book, and download its code, at http://www.LarryUllman.com.

Getting Started

To start, you’ll need to add a database table to the site, something like:

This article assumes you’re using PHP with MySQL. If you’re not, the actual code you’d use would differ but the base theories would be the same.

That SQL command represents the crux of such a table. The two most important columns are the rating, which will be an unsigned tiny integer (perhaps from 1 to 5), and the item_id. Most likely, your particular column won’t be called item_id, but perhaps page_id or product_id or whatever. The important thing is that item_idwhatever you call itacts as a foreign key to the thing on your site that’s being rated, be it a product or a page of content.

The date_created column isn’t required, but I generally like to timestamp almost every record added to every database. You never know when that information will come in handy!

Important decisions to be made involve who can rate content and how many times. By the latter I mean: Will a system be implemented for guaranteeing (or attempting to guarantee) that a person doesn’t rate the same item multiple times? Doing so isn’t hard, but requires some thought.

The first option is not to reflect the rater at all, trusting that the same person won’t make rate an item multiple times (or, if they do, it’s not a big deal). On the opposite end of the spectrum, perhaps only registered (and therefore logged-in) users can provide ratings. In that case, the ratings table should also store the user ID, or whatever represents a foreign key to the applicable users table. You would also change the table’s definition to place a unique index on the combination of the user’s ID and the item ID, or just use those two columns as the primary key, which will have the same effect:

If registration is not required in order to rate items, which is really for the best, you’ll need another way to identify users. Probably the best solution would be to store a cookie in the user’s browser with a unique ID. You could then store the ID in the ratings table, under the user_id field, but most likely as a CHAR(32). The PRIMARY KEY index would still apply, preventing the same person from rating the same item more than once (you’d want to add PHP code to the page reflecting the visitor’s previous rating anyway).

NOTE

If you were to go the cookie route (storing a unique user ID), you’d need to make sure the cookie has a long lifea late expirationand that it is renewed with each visit to the site.

The next consideration is a user interface issue: How will the visitor indicate his or her rating? The simple route is a select menu or series of radio buttons. With the right CSS, that can look pretty good. Alternatively, you can use a series of images: stars or the like. Which route you go will change how you paste Ajax onto the system. In the rest of the article, I’ll discuss the form option.