Per Søderlind

I code for fun

Facebook ajax rating in php

I’ve written an ajax rating snippet, you’ll find a demo at

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’]
< ?php // the $callbackroot MUST point to your server, if you point it to your, ajax will fail. $callbackroot='http://[PLACE_YOUR_CALLBACK_URL_HERE]'; // do not add a trailing slash e.g.: //AJAX Response (should be first php or in a separate php file): if (isset($_REQUEST['rate'])) { //do something and return "ok" if things are fine echo "ok"; exit; //Important! Stops remaining php after AJAX request is processed }//what ever apicall you use to authenticate, this one is from smiley: $fb = get_fb(); $user = $fb->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 };'< ?=$callbackroot?>/rate.php',params); }


A how-to and the rate.css are located at


My digital tool chest


Microsoft Hyper-V gotcha


  1. Frode


    Can I use this?

Leave a Reply

Powered by WordPress & Theme by Anders Norén