Help: Ajax rating with CStarRating

I use CStarRating to create a ajax vote system, how can I perform an ajax function to send the rating to server

In the code below, I dont know how to get the rating value, please help me


<?php $this->widget('CStarRating',array('name'=>'rating', 'value'=>'$model->rating', 'callback'=>'$(this).click(function(){$.ajax({

   type: "POST",

   url: "'.Yii::app()->createUrl('post/rating').'",

   data: "id='.$model->id.'&rate=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />",

   success: function(msg){

     alert( "Data Saved: " + msg );

   }

 })})'));?>



thanks

In this case you can do:




data: "id='.$model->id.'&rate="+ $(this).val()



I did but not work :(

Oh yeah sorry, also take out the $(this).click part, so it will be like this:




'callback'=>'function() {

  blahblahblah

}'



This works for me…




<?php $this->widget('CStarRating',array(

    'name'=>'rating',

    'callback'=>'

        function(){

        $.ajax({

            type: "POST",

            url: "'.Yii::app()->createUrl('book/rating').'",

            data: "id='.$model->id.'&rate=" + $(this).val(),

            success: function(msg){

                alert( "Data Saved: " + msg 

            )

        }})}'

));?>



An example in the cookbook would be nice… :P

Although im still wondering if i really want to let PHP write my JS… I loose all IDE functionality and it does not really look any easier :)

I got some errors last night doing the example above, I cant remember what hey where though, cause I solved it:p

But thought I could share what I did just in case anyone else googles this:p

Note! I use this in a _view file used by a dataprovider, that is why I use $data->id insteaad of $model->id.

And since the code runs in a lop the id’s had to be diffrent each time.

Now you know that.

What my code does is it sends the vote and id as two get parameters to my rating action in the howto controller,

if the controller returns success it updates a div with "Thank you for voting", then the div fades in slowly,the script pauses for 5 seconds(5000 milliseconds) and then fades out again.


<?php

	$this->widget('CStarRating',array(

    'name'=>'rating'.$data->id,

    'callback'=>'

        function(){

        	url = "/howto/rating";

			jQuery.getJSON(url, {id: '.$data->id.', val: $(this).val()}, function(data) {

				if (data.status == "success"){

			$("#rating_success_'.$data->id.'").html(data.div);

			$("#rating_success_'.$data->id.'").fadeIn("slow");		

			var pause = setTimeout("$(\"#rating_success_'.$data->id.'\").fadeOut(\"slow\")",5000);

					}

				});}'

			));

?> 	<div id="rating_success_<?=$data->id;?>" style="display:none"></div>

And my controller:




public function actionRating()

	{

		if ( Yii::app()->request->isAjaxRequest )

		{

			$model = $this->loadModel( $_GET['id'] );	

			$model->rating = $_GET['val'];

			$model->save();

			echo CJSON::encode( array (

						'status'=>'success', 

						'div'=>'Thank you for voting!',					

						) );

		}

	}




NOTE! Whats missing is to calculating an average score and present it. I leave that for each person to solve, I haven’t thought about how I want to do it yet so.

@mech7, Thanks man!