Welcome Guest, Not a member yet? Register   Sign In
Mootools Rapid Ratings : Passing variable to Php via the Request class [Tuto]

Solution :

After some good time reading the mootools documentation, i found the solution to my problem and here the solution using the rabid rating widget (You can implement it to codeigniter easily :
This the widget :

First, i'm french Tongue and here is a simple example (tuto) :
File to edit :
demo.php, ratings.php, rating.js

First we are going to add a new field to the rabid_ratings table called user_id.
demo.php :
Put the user id in a hidden input :
<input  type="hidden" id="userid" value="<?php echo $row->id ;?>" />
$row->id or wtv.


Line 80 :

we are going to add a new parameter to the ajax post method and select the input id (id="userid") to grab it's value.
.post({vote:votePercent,id:el.ratableId,'someid': $('userid').value});
If you want to check if the value is passed via ajax, add an alert to print the value :
After line 71 :

ratings.php :

Let's grab the value via the $_POST method in php :
Line 235 :
$r->doVote($_POST['id'], $_POST['vote']);
add :
$r->doVote($_POST['id'], $_POST['vote'],$_POST['someid']);

add a new parameter to the doVote function :
function doVote($ratableId, $percent,$data)
$userid = $data;
$SQL_INSERT_VOTE = "INSERT INTO $this->ratings(ratable_id, ip_address, user_id, ".
            "rating) VALUES ('$id', '$ip', '$userid'  , '$rating');";


----------------------- That all Smile -----------------------------------------------
Really easy Tongue!

Hi there ,

I really need your help guys, im stuck stuck stuck!! I'm using rabidratings widget! I create a the library and everythings going smooth. Now i want to pass the user id to the doVote function to store the id of the voter :
     * RabidRatings::doVote
     * This is the function in charge of handling a vote and saving it to the
     * database.
     * NOTE: This method is meant to be called as part of an AJAX request.  As
     * such, it unitlizes the die() function to display its errors.  THIS
     * @param integer id      - The id of key to register a rating for.
     * @param integer percent - The rating in percentages.
    function doVote($ratableId, $percent) {
         echo $this->profid  ;
        $ip = $_SERVER['REMOTE_ADDR'];
        //Make sure that the ratable ID is a number and not something crazy.
        if (is_numeric($ratableId)) {
            $id = $ratableId;
        } else {
            die("ERROR: Id invalid.");

        //Make sure the percent is a number and under 100.
        if (is_numeric($percent) && $percent < 101) {
            $rating = $percent;
        } else {
            die("ERROR: Rating percent invalid.");

        //Insert the data.
        $SQL_INSERT_VOTE = "INSERT INTO $this->ratings(ratable_id, ip_address, ".
            "rating) VALUES ('$id', '$ip', '$rating');";

        //Die with an error if the insert fails (duplicate IP for a vote).
        if (!mysql_query($SQL_INSERT_VOTE)) {
            die("ERROR: Duplicate votes not allowed.");

        $rating = $this->loadRating($id);
        echo $this->getStarMessage($rating);

I'm not really good in ajax (Im a newbie).. the dovote function is called like :

if (isset($_POST['vote']) && isset($_POST['id']))
        $this->doVote($_POST['id'], $_POST['vote']);

In the js file :

window.addEvent('domready', function(e) {
    var rating = new RabidRatings({url:'http://localhost/projectolearn/index.php/users/loadlib'});

HERE : I passed the user id in a var to the RabidRating class, but when the ajax call executed to store the vote, it reset the user id variable...

I get the user id from the view, samething goes to the call vote :

&lt;?php echo $row->id; ?&gt; //user id

    $rr = new RabidRatings() ;
        for ($i = 1; $i < 5; $i++) {

What i want is to pass the user id to the doVote function! I passed almost 5 hours to find a solution or a hack! But no way..

Please help me! sorry for my poor english Smile


Help please!

Anytime you are dealing with js & ajax, do yourself a favor and checkout [url="http://api.jquery.com/jQuery.ajax/"]jQuery[/url]. It will save your life. The tricky part with ajax is that different browsers behave differently. You have to write routines for IE/mozilla/webkit. That's were jQuery comes in as it is cross browser. I don't know if you are trying to reference a js class with
var rating = new RabidRatings({url:'http://localhost/projectolearn/index.php/users/loadlib'});

You are using similar syntax in other spots using php. The way I have code some simple voting widgets with ajax is creating the form with options. Use jQuery to submit the form with the selected option to the server. The ajax call goes to my CI controller where I check user session, poll id, and vote id. I then submit values to my model if successfully. Send appropriate data back to user once voted has been counted (like results).

Hi pickupman,

Thanks for you reply! I started learning jquery last week but i found that rabidratings (Using mootools javascript framework) is a good widget. i'll try jquery with the five star rating plugins when i feel ready to jquery.

Well, with
var rating = new RabidRatings({url:'http://localhost/projectolearn/index.php/users/loadlib'});

i'm refering to the rabidratigns library that i create from the default rabidrating class code. The vote work fine, it store all data in the database, what i need is just to pass other variables to the database with the user vote...

This my code :

&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Loadlib extends Public_Controller
    public function __construct()
        // Call the parent's constructor method
          // Load the required classes
    public function index()
        echo "blah test " ;

You can take a look to the rabidrating codes :

Thanks dude.

You have two options:
1. Add a variable to the js for rabidratings. It has a few default options declared at lines 22-27.
2. When the rating is posted to your controller or library just use the session class or your auth library to add a user id.

On a side note, any particular reason for not using CI's active record (db) class? You are using plain queries.

Thanks dude for your reply! No, no particular reason! I let the rabidrating class as it is. Well, as u told me, i'm going to try jquery with this rate widget:

and i'll try what u suggest me for the rabidrating ^_^

Thank you !

The nice thing about this one is that is a little more fine grained control. You can modify the callback to send your data. Looking at the api example, they use .getJSON to submit the vote. I would recommend using $.post, and you can specify the json data type for the response from the server. CI plays nicer with POST data.

Yeah i agree with you! Can't wait to implement it on codeigniter Smile!! cya bro and thans again

Fixed! Check first post, i shared my solution to this problem.

Theme © iAndrew 2016 - Forum software by © MyBB