如何在javaScript中刷新页面后更新数据库中表的星级速率? [重复]

如何在javaScript中刷新页面后更新数据库中表的星级速率?  [重复]

问题描述:

I want give the opportunity to the user to change the rating value of a product, but it is not working after refreshing the page. For example, when he goes to a new product that he did not rate, he can change the rating value again and again before refreshing the page. But after refreshing the page he can change the rating value of the same product, but the alert for the "ratingValue2" is not working and the database is not updating.

Here is my PHP code.

$jsqla3 = mysql_query("select * from user_star_rate where product_id='$product_id' and email='$visit_email'") or die(mysql_error());
$jfeta3 = mysql_fetch_assoc($jsqla3);

if($jfeta3 != null) {
  $ratea = $jfeta3['rate_value'];
} 

Here is my HTML code.

<input class="rating form-control input-star-rate" id="<?php echo $rateid; ?>" name="rating" value="<?php echo $ratea; ?>" data-min="0" data-max="5" data-step="0.3" data-size="xs" style="display: none; text-align: center;"/>

Here is my javaScript code.

$(function(){
 $(document).ready(function(e) {
   var $stars = $('.input-star-rate');

   $stars.bind('change', function() {
      var $this = $(this); 
      var ratingValue = $this.val();
      var ratingValue2 = parseFloat(ratingValue);
      alert(ratingValue2);
   });
 });
});
</div>

这个问题是 完全重复: p>

  • 如何在javaScript中刷新页面后更新数据库? li> ul> div>

    我想让用户有机会更改产品的评级值,但刷新页面后它无法正常工作。 例如,当他访问他没有评价的新产品时,他可以在刷新页面之前反复更改评级值。 但刷新页面后,他可以更改同一产品的评级值,但“ratingValue2”的警报不起作用且数据库没有更新。 p>

    这是我的PHP代码 。 p>

      $ jsqla3 = mysql_query(“select * from user_star_rate where product_id ='$ product_id'和email ='$ visit_email'”)或死(mysql_error()); \  n $ jfeta3 = mysql_fetch_assoc($ jsqla3); 
     
    if($ jfeta3!= null){
     $ ratea = $ jfeta3 ['rate_value']; 
    } 
      code>  pre> 
      
     

    这是我的HTML代码。 p>

     &lt; input class =“rating form-control input-star-rate”id =“&lt;?php echo $  rateid;?&gt;“  name =“rating”value =“&lt;?php echo $ ratea;?&gt;”  data-min =“0”data-max =“5”data-step =“0.3”data-size =“xs”style =“display:none; text-align:center;”/&gt; 
      code  >  pre> 
     
     

    这是我的javaScript代码。 p>

      $(function(){
     $(document).ready(function(e  ){
     var $ stars = $('。input-star-rate'); 
     
     $ stars.bind('change',function(){
     var $ this = $(this); 
      var ratingValue = $ this.val(); 
     var ratingValue2 = parseFloat(ratingValue); 
     alert(ratingValue2); 
    }); 
    }); 
    }); 
      code>   pre> 
      div>

At first you should remove the document.ready() as you have already used its shorthand.

And for your problem on refreshing the page you are not getting the updated rate then you need to update the database on changing the star rating by using $.ajax() something like,

$(function(){
    var $stars = $('.input-star-rate');

    $stars.bind('change', function() {
      var $this = $(this); 
      var ratingValue = $this.val();
      var ratingValue2 = parseFloat(ratingValue);
      alert(ratingValue2);
      $.ajax({
         url:'update_user_rate.php',data:{rate:ratingValue},
         success:function(data){
             alert(data);// you can apply check for success or error
         }
      });
    });
});

Also in the update_user_rate.php page you need to use query to update the database, so that you will get the updated user rate next time when you refresh the page.