2016-05-01 16 views
0

我在我的應用程序中使用了這個rateyo插件。有一個用戶可以給實體評級的頁面。但在給予評級之前,我正在進行檢查,如果該用戶已經爲該特定實體分配了評級,並且該用戶確實給予了評級,那麼我從數據庫獲取評級值並將其設置爲該插件所述。但如果是新用戶,他可以提交新的評分。使用rateyo插件設置數據庫評級值

這裏是我實現的代碼:

$(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>" }); 
       } 
      }); 
     }); 
下面

是設置評價值,如果它存在的代碼。

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

問題是,當設置舊的評級時,會進行ajax調用,該調用將在表中再次插入相同的數據。我只希望我的ajax調用只在需要設置新評級或編輯以前的評級時才能使用。我無法爲此找到出路。請幫忙。

回答

1

當您在rateyo初始化後設置評級時,將在該元素上觸發rateyo.set事件。

請不要那樣做,而是如果已經老了評級存在,初始化過程本身通過它像

$(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']應包含0如果用戶沒有給出任何評價之前。