Art AboutFAQWrite for usContact InfoRSS Feed
Search A/R/T:

Writing an AJAX-based Visual Rating System with PHP

by Marco Tabini — Page 1 of 5
Click to rate:    
3,370 votes / avg. rating 57.35%

AJAX and PHP give developers the perfect platform for creating interactive tools that allow users to perform non-critical tasks without having to reload a page. In this article, Marco Tabini illustrates how you can create a cool-looking rating system for virtually any web page with nothing more than a handful of PHP, a touch of Javascript and a couple of images.


 

A rating system is a great tool for any website. It serves two important purposes: first, it gives your users a way to interact with your pages—and interactivity is an important aspect of any website these days. Second, it provides you with a valuable feedback tool that you can use to gauge viewer interest in individual areas of your site. In other words... everybody wins!

You can download the code for this article here.

AJAX as the Perfect Tool

There are a number of reasons why AJAX is perfect for creating a rating system:

  • It's not a mission-critical system, so you don't need to worry about visitors being unable to use it because their browser is not AJAX-ready
  • Immediate feedback to the user is not only beneficial, but also desired—there is no advantage to having to refresh an entire page just because the user has cast his or her vote
  • AJAX allows you to provide a very intuitive interface that doesn't require the use of forms—therefore, there is never any wasted page real estate, since the same visual controls are used to cast a vote and to display the current rating.

The Concept

We want our rating system to be visual—providing the user with a means of both determining the average rating of a web page and casting his or her own vote using the mouse. The easiest way to do so is to create an image that visually represents the rating like the one shown here on the top-right; a user can click on this image to indicate his or her own vote—the further right he or she clicks, the higher the vote. Our image “happens” to be 100 pixels wide—perfect for a percentual rating; however, this is just as arbitrary as any other rating system, and you could decide to make yours completely different.

One of the most important aspects of AJAX-powered websites is the fact that you must provide the user with feedback every step of the way. In our case, we want the user to be able to click on the image to cast his or her vote; when this happens, we will transparently send a request to our web server, which, however, will not be immediately visible to the user, since his or her browser will not provide any kind of visual feedback (although some browsers may, we should assume that all will). To solve this problem, we will display a little “Saving...” box at the top of the screen—just like GMail does when you send an e-mail—and pop up a dialogue box when the voting process is complete.

 


 Tags:  , , , , , , , , ,
 Add tag(s) (comma separated):
[Tags beta] — [Add New][Help]  

Tags Help

Tags are keywords associated with a web page that help classifying information. You can find a good explanation here.

To add one or more tags to this page, simply enter them below (separate them with a comma) and hit enter or click on the "Go" button.

New Comment
This form allows you to type in a new comment. Keep in mind the following:
  • The system accepts input in plain text format. Newlines will be converted to the HTML equivalent, and the system will try to catch most URLs and make them clickable.
  • Your e-mail address will never be displayed. We will use it only to notify you when new comments are posted to this page.
  • As a rule, we do not delete comments unless they are offensive, racist, spam or otherwise inappropriate.
  • Bold fields are required
Your Name:
Your e-mail:
Type this number:
Subject:
Comment:
Comments   New Comment
Re: Writing an AJAX-based Visual Rating System with PHP (#128)
By Chauncey Thorn on 2006-06-13 12:14:32

Archive ajax-rating-system.zip is broken.

[Reply to this]

Re: Re: Writing an AJAX-based Visual Rating System with PHP (#129)
By Marcus on 2006-06-13 13:05:28

My apologies.

It is fixed now. Thanks for letting us know and for reading!

- Marcus

[Reply to this]

Re: Re: Re: Writing an AJAX-based Visual Rating System with PHP (#130)
By Ilia Mogilevsky on 2006-06-13 19:19:48

Helpfull article! Go Ajax!

[Reply to this]

[Comment deleted by administrator] (#347)
By dsf on 2006-10-17 07:50:02

This comment was deleted by the system administrator

[Reply to this]

Re: Writing an AJAX-based Visual Rating System with PHP (#147)
By james on 2006-06-22 14:58:28

could you include what browsers are surpoted

[Reply to this]

Re: Re: Writing an AJAX-based Visual Rating System with PHP (#148)
By Marco on 2006-06-22 22:25:22

Sure—since my system uses the YUI library, it essentially provides the same level of browser support:

http://developer.yahoo.com/yui/articles/gbs/gbs.html

[Reply to this]

Re: Writing an AJAX-based Visual Rating System with PHP (#157)
By theta on 2006-06-29 17:05:34

is there a online demo?

[Reply to this]

Re: Re: Writing an AJAX-based Visual Rating System with PHP (#158)
By Marco Tabini on 2006-06-29 22:13:51

No, but the rating system that we use (look at the top of this page) works in a very similar way.

Thanks!


Marco

[Reply to this]

Re: Re: Writing an AJAX-based Visual Rating System with PHP (#533)
By nima on 2007-05-10 20:57:25
I get error!! (#276)
By andrei on 2006-08-04 14:40:19

I get error (please see: http://www.vatif.com/rater ) after I uploaded the files... and make the changes (database, paths).

Could you please let me know how can I fix it. 10x!!

[Reply to this]

[Comment deleted by administrator] (#333)
By hahahahah on 2006-09-23 11:31:38

This comment was deleted by the system administrator

[Reply to this]

Re: Writing an AJAX-based Visual Rating System with PHP (#335)
By gui on 2006-09-27 14:00:15

Hi, thanks for great article... is there a sql dump of database ?

[Reply to this]

Re: Writing an AJAX-based Visual Rating System with PHP (#481)
By mo on 2007-02-04 07:16:12

the image doesn't get displayed, it doesn't work

[Reply to this]

Re: Re: Writing an AJAX-based Visual Rating System with PHP (#508)
By Stephanie on 2007-03-28 03:45:16

Awesome script, thanks!

[Reply to this]

Re: Writing an AJAX-based Visual Rating System with PHP (#516)
By Aran on 2007-04-09 09:18:39

Great Idea, will improve number of ratings tnx. Aran www.aran.com

[Reply to this]

Re: Writing an AJAX-based Visual Rating System with PHP (#556)
By niiconn on 2007-06-16 06:29:31

Hi, thanks for this nice script. it works well, but I have a question: How can i modify the unique pageID? Because, my page works with dynamic pages (i.e. page.php?page_id=1), so if i cast a vote I can only cast one time for page.php..

Bye
Rob

[Reply to this]

Re: Writing an AJAX-based Visual Rating System with PHP (#602)
By sdfs on 2007-10-16 17:37:59

asdfasd

[Reply to this]

Re: Writing an AJAX-based Visual Rating System with PHP (#608)
By John on 2007-10-26 18:32:11

Another ajax rating and review system you might be interested: http://www.rating-system.com

Cheers

[Reply to this]

Index