0

I am using this rateyo plugin in my application. There is a page where user can give rating to an entity. But before giving rating I am making a check, if that user already assigned a rating to that particular entity and if the user does, then I fetch rating value from database and set it as the plugin says. But if it is a new user, he can submit his new rating.

here is the code what I am implementing:

$(document).ready(function(){
$("#rateYo").rateYo().on('rateyo.set',function(e, data){
            $.ajax({
                type : "post",
                url : "PostRatingValue.php",
                data : {
                    "rating" : data.rating,
                    "peopleid" : celeb_id,
                    "userid" : user_id
                },
                dataType : "json",
                success : function(response){
                    if(response.toString() == "true"){

                        $.growl.notice({ title: "Growl", message: "Rating<b> "+ data.rating +" </b>assigned successfully" });
                    }
                    else{
                        $.growl.error({ message: "<b>Unable to assign rating.</b>" });
                    }
                },
                error : function(response){
                    $.growl.error({ message: "<b>Something went terribly wrong! :'(.</b>" });
                }
            });
        });

below is the code to set the rating value if it exist.

<?php
            if(isset($fetchRatingIdAndValue['ratingpoints'])){
        ?>  
                $("#rateYo").rateYo("option", "rating", <?php echo $fetchRatingIdAndValue['ratingpoints'];?>);
        <?php       
            }
        ?>
});

Problem is that, when old rating is set, an ajax call is made which inserts same data again in table. I want my ajax call to work only when I need to set new rating or edit previous rating. I am not able to find the way out for this. Please help.

Amit Kaushal
  • 429
  • 1
  • 9
  • 25
  • See how it works here: https://stackoverflow.com/questions/45523742/how-can-i-use-rate-yo-jquery-star-rating-plugin-with-data-attribute/57359306#57359306 –  Aug 06 '19 at 11:55

1 Answers1

1

When you set the rating after Initialization of rateyo, rateyo.set event will be fired on the element.

Please do not do that, instead If already old rating exists, pass it during initialization itself like

$(document).ready(function(){
    $("#rateYo").rateYo({

      rating: <?php echo $fetchRatingIdAndValue['ratingpoints'];?>
    }).on('rateyo.set',function(e, data){

        $.ajax({
            type : "post",
            url : "PostRatingValue.php",
            data : {
                "rating" : data.rating,
                "peopleid" : celeb_id,
                "userid" : user_id
            },
            dataType : "json",
            success : function(response){
                if(response.toString() == "true"){

                    $.growl.notice({ title: "Growl", message: "Rating<b> "+ data.rating +" </b>assigned successfully" });
                }
                else{
                    $.growl.error({ message: "<b>Unable to assign rating.</b>" });
                }
            },
            error : function(response){
                $.growl.error({ message: "<b>Something went terribly wrong! :'(.</b>" });
            }
        });
    });
});

$fetchRatingIdAndValue['ratingpoints'] should contain 0 if user has not given any rating previously.