Per Søderlind

I code for fun

  1. general
  2. Facebook ajax rating in php

Facebook ajax rating in php

I’ve written an ajax rating snippet, you’ll find a demo at http://apps.new.facebook.com/ajaxrating/

It is based on Paul OB’s CSS Star Matrix Pre-loaded

The source code below, should be self explanatory, but if you need input on how to do fbjs ajax, you’ll find documentation and examples over at the facebook development site

[sourcecode language=’php’]
require_login();

?>
function save_rating(rate) { // array with the class names, used when you add the new class var ratings = [ "nostar", "onestar", "twostar", "threestar", "fourstar", "fivestar" ]; var ajax = new Ajax(); ajax.responseType = Ajax.RAW; ajax.ondone = function(data) { if (data == "ok") { // remove old class names (I don't know which one, so I remove all of them), and add the new document.getElementById('rating_1').removeClassName('nostar').removeClassName('onestar').removeClassName('twostar').removeClassName('threestar').removeClassName('fourstar').removeClassName('fivestar').addClassName(ratings[rate]); } else { new Dialog().showMessage('Dialog', 'set rating failed'); } } ajax.onerror = function() { new Dialog().showMessage('Dialog', 'set rating failed'); }; ajax.requireLogin = 1; var params={"rate":rate }; ajax.post('/rate.php',params); }

[/sourcecode]

A how-to and the rate.css are located at http://forum.developers.facebook.com/viewtopic.php?id=22547

Previous

My digital tool chest

Next

Microsoft Hyper-V gotcha

2 Comments

  1. Frode

    Nice!

    Can I use this?

Leave a Reply

Powered by WordPress & Theme by Anders Norén