Communication problem between ECHO and ALERT

Hi, I’m using the CStarRating class but I can’t show in alert() the data I pass by echo() via an action.

Here the code of the widget:


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

    'model' => Offers::model(),

    'attribute' => 'rating',

    'readOnly' => false,

    'htmlOptions' => array('id'=>'offer_'.$data->id),

    'maxRating' => 5,

    'callback' => '

        function() {

            $.post(

                "'.Yii::app()->createUrl('offers/index').'",

                "action=rate&stars="+$(this).val()+"&offerID='.$data->id.'",

                function(msg) { alert(msg); }

            );

        }

    ',

));

and here the code of the action:


if(isset($_POST['action'])) {

    if($_POST['action'] == 'rate') {                

        $stars = filter_input(INPUT_POST, 'stars', FILTER_SANITIZE_NUMBER_INT);

        $offerID = filter_input(INPUT_POST, 'offerID', FILTER_SANITIZE_NUMBER_INT);

        $offer = Offers::model()->findByPk($offerID);

        $rating = $offer->rating;

        $reviews = $offer->reviews;

        $offer->rating = round(($rating * $reviews + $stars) / ($reviews + 1));

        $offer->reviews = $reviews + 1;

        $offer->save();

        echo $offer->rating;

    }

}

Instead of showing $offer->rating the alert shows the whole page source code.

What’s the problem? Thank you for your support.

The problem was render() before the echo() statement :rolleyes:

Now I’ve moved render() at the action bottom and put Yii::app()->end() after the echo() and it works

Hello,

Maybe some pointers?

  • Have you checked the ajax request’s form data in Firebug? Are they what you intended to post?

  • Have you checked the results of your ajax request in Firebug? Are they really the source code like you see in your alert box?

Edit: Ok I see you’ve solved it, but there was no “render” in your originally posted code

Yes, you’re right, my fault, the render() was before the initial if():


if(isset($_POST['action'])) {