Remember Me
forgot your password?

Ajax Rating Script - Php & Mysql

Introduction

Frequent visitors of ajax enabled websites, like ajaxian, have all witnessed them already: ajax rating widgets. They are flashy, animated, you can use them to rate the content (usually without refreshing the page) and if you could, you'd present them to your parents and marry them. Compared to the classic rating system, as on IMDb, they incite people to click them, reducing the effective rating process to only one click.
In this tutorial, I want to show you how to create the JavaScript framework to display the animated rating widget and how to connect it to your server backend by using some of the most common Ajax frameworks out there. I clearly separate the page creation from the JavaScript functions and the rating backend, to allow the script to be as flexible as possible and to be easy integrable into your existing website.
This tutorial is not meant to present you with a finished script (even though you could simply copy&paste the end result into your website and make it work without any problems), but rather to explain the design and implementation process that would enable you to create your own widgets if you'd need to. Getting started with the HTML markup

What It Is

This is a rating bar script done with PHP and mySQL that allows users to rate things like can be done all web 2.0-like with no page refresh. It is a major improvement on the previous version because it is now unobtrusive, meaning that if Javascript is off it will still work (although the page will refresh). You can also set the number of rating units you want to use (i.e. 4 stars, 5 stars, or 10 stars) on a rater to rater basis (see samples below or read the docs). A few other changes were made as well  see the docs for details. Note that this script isnt tied to any specific system (such as WordPress), so you should be able to adapt it to your situation without too much trouble. What are you waiting for? Check the Ajax Rating demos.

It is most important that there are no line breaks in this code, as this will complicate the DOM tree unnecessarily. If you are uncertain about what I just said, please read the W3Schools HTML DOM Tutorial for further reference, since we are going to access the DOM directly from within our JavaScript.
As you can see, the div container is pretty easy to generate in the server-side scripting language, requiring most of the time only one line:
printf("%s", ratingId, rating);

The "continue" keyword in our JavaScript code, just like in most other programming languages, continues with the next iteration of the loop and prevents the execution of the rest of the code during that iteration. Read the rating value

Now that we have all the rating containers, we can start by reading the rating value written as text inside the div container. To read that value, we access the first child node of the div container, which is a TextNode, and access its nodeValue, which returns the text CDATA in case of text nodes. This is done by the following line:
var rating = ratings[i].firstChild.nodeValue;

There is no graceful way to recover from that error, so I simply decided to continue with the next rating container and prevent that error from happening within the server backend.
Now, we need to loop over the number of stars that are displayed and check what the image graphic will be that is displayed on the star. Using the HTML DOM function createElement(), we initialize a new image and progressively add the respective values to that element. Of course, the first thing we are interested in is the displayed image: we could either test the rating against the current iteration value or we could decrement the rating value during each iteration and test against 1, 0.5 and 0. I decided to decrement the rating during each iteration, which presents me with three simple cases to test: if the rating is greater than or equal than 1, the star is "on", if the rating is exactly 0.5, the star is "half" otherwise, the star is "off". This results in the following code:
for (var j = 0; j < NUMBER_OF_STARS; j++)
{
var star = document.createElement('img');
if (rating >= 1)
{
star.setAttribute('src', './images/stars/rating_on.gif');
rating--;
}
else if(rating == 0.5)
{
star.setAttribute('src', './images/stars/rating_half.gif');
rating = 0;
}
else
{
star.setAttribute('src', './images/stars/rating_off.gif');
}
ratings[i].appendChild(star);
}

Conclusion

Our current JavaScript allows us to transform specially marked div containers into animated rating widgets that we can use in specialized Ajax frameworks to link to our server backend. The HTML markup is completely separated from the JavaScript code, which will leave the user with a not very stylish but still visual display of the current rating in case JavaScript is disabled. I've put together a ZIP file with the current result, containing an HTML file, a CSS file, the JavaScript file and the images. Connect the widget to the server with different frameworks

Please make sure to delete the "window.onload=init_rating;" line in the "script.js" file if you downloaded the .zip file, since we're using the specialized framework onload event.
The examples below are only an illustration of how this given task is achievable with a variety of different JavaScript frameworks. It should not be used as a reference about what framework is superior since not every framework follows the same route and has the same objectives. Before jumping to conclusion, you should read more sources and also hear every site. There are good reasons for dojo not having the $ operator and some interesting points can be found here (in the comments). When you need to decide which framework to chose, base your decision on your specific task and the framework you're most comfortable with. The PHP backend

Asif Khalyani

I am Asif Khalyani. I am software Engineer. PHP and ajax free script download site phpasks. User can download ajax rating script

Rate this Article: 3.9 / 5 stars - 123 vote(s)
Print Email Re-Publish

Add new Comment



Captcha

  • Latest Programming Articles
  • More from Asif Khalyani

Web Portal Development

By: Digisha Modi | 07/01/2010
Web portals and Content Management Systems help improve on customer satisfaction and customer loyalty. By providing valuable information and services to customers on line, there is a much greater potential for increased sales and growth. Of course it is critical to maintain other forms of customer service, but in today's technology environment, more people are going to the internet as their first point of contact with businesses of all sizes.

Web Portal Development

By: Digisha Modi | 07/01/2010
Web portals and Content Management Systems help improve on customer satisfaction and customer loyalty. By providing valuable information and services to customers on line, there is a much greater potential for increased sales and growth. Of course it is critical to maintain other forms of customer service, but in today's technology environment, more people are going to the internet as their first point of contact with businesses of all sizes.

XHTML Development

By: Digisha Modi | 07/01/2010
XHTML is the W3C standard for developing cross browser compatible web sites. It fuses all of the display elements of HTML with some of the functionality of XML. By forcing a web designer to program more carefully and adhere to strict code standards, XHTML allows a larger percentage of browsers or code parsers to properly parse your documents. In less geeky terms, it means more people can see your site the way you meant for them to see it, and less people see your site with elements strewn all ov

Magento Custom Module Development

By: Digisha Modi | 07/01/2010
Magento is a new and powerful eCommerce system offering rich customization possibilities by extensions and modules. Magento is built on a fully modular model that influences an unlimited scalability and flexibility for your store. Part of customizing Magento is, creating Magento custom Modules. These allow you to inject functionality anywhere, whether in a "static" block fashion that's more than static, or a shipping/payment module, or large module to do something as large as integrating a 3rd p

Winnovative HTML to PDF Converter - An Easy Way to Create and Maintain PDF Reports

By: Winnovative Software | 06/01/2010
The HTML to PDF Converter for .NET from Winnovative Software combines the powerfull printing capabilities of the PDF format with the flexibility of the HTML to format to create a great tool for creating nicely formatted and easy to maintain PDF reports and documents.

ASP.NET Reports creation

By: Famous16 | 06/01/2010
The ability to create reports is one skill that all must have had to develop. No matter the situation, school work or even in the home, a report could be necessary in almost any circumstance.

SilverlightRS - an upgrade to Silverlight

By: chaudhary fahim | 06/01/2010
Windows Presentation Foundation was the initial name for Silverlight everywhere.

The “MySQL” Way of the World

By: Tyler Moon | 06/01/2010
Each enhancement of the again and again points out to the fact as to why the newest version of MySQL is the perfect choice for IT professionals who want to profit from the flexibility and cost savings of open source software, but not forfeit anything relating to database reliability, performance, and ease-usability features.

Easy Ajax Inline Text Edit 2.0

By: Asif Khalyani | 09/09/2008 | Programming
A small piece of javascript reads al SPAN tags, checks if it has class="editText" and a id=. If that is true, it adds a onclick function. That onclick function will create a textfield or input (depending on the size of the editable text). Someone has the ability to edit the field. When the text field is blurred, it will read the contents, and starts a XMLHttpRequest and ‘sends’ the content + fieldname + any set vars to an update file.

Ajax Rating Script - Php & Mysql

By: Asif Khalyani | 09/09/2008 | Programming
Frequent visitors of ajax enabled websites, like ajaxian, have all witnessed them already: ajax rating widgets.

Crop Image File Size, Width & Height Using Php Script

By: Asif Khalyani | 03/07/2008 | Programming
Cropping and resizing your images for the Web is a common technique for creating smaller thumbnail images that download quickly.

Ajax Form Validation and Thread-safe Ajax

By: Asif Khalyani | 03/07/2008 | Programming
This is the PHP and AJAX form validation application you can create AJAX and PHP: Building Responsive Web Applications.

Submit Your Articles Free: Signup
Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (0.13, 1, w2)