dtcmadj31951
2014-11-26 08:49
浏览 255
已采纳

如何从jQuery中的数据库中获取数据?

The values do not pass for "id" and "rate_number" from database."ratingValue" is working, but "id" alert and "num" alert are not working.

Here is my PHP code.

$jsqla = mysql_query("select id,name,rate_score,rate_number,video_image from products where genre='$genre' limit 0,5");

if($jrowa['rate_number'] > 0){ 
  $ratea = $jrowa['rate_score'] / $jrowa['rate_number']; 
}else{ 
  $ratea = 0; 
}

Here is HTML code.

<div class="col-sm-2 portfolio-item" style="width: 20%;">

    <input class="rating form-control input-star-rate" 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;"/>

</div>

Here is jQuery code.

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

        $stars.bind('change', function() {
            var $this = $(this); 

            var ratingValue = $this.val();
            alert(ratingValue);

            var id = $this.attr("id");
            alert(id);

            var num = parseInt("$this.attr("rate_number")")+1;
            alert(num);
        });
    });
});

图片转代码服务由CSDN问答提供 功能建议

这些值不会从数据库传递“id”和“rate_number”。“ratingValue”正在运行,但是“ id“alert and”num“alert not not working。

这是我的PHP代码。

  $ jsqla = mysql_query(“select id,name,rate_score,rate_number,video_image from products where  genre ='$ genre'limit 0,5“); 
 
if($ jrowa ['rate_number']&gt; 0){
 $ ratea = $ jrowa ['rate_score'] / $ jrowa ['rate_number']  ;  
}其他{
 $ ratea = 0;  
} 
   
 
 

这是HTML代码。

 &lt; div class =“col-sm-2  portfolio-item“style =”width:20%;“&gt; 
 
&lt; input class =”rating form-control input-star-rate“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; 
 
&lt;  / div&gt; 
   
 
 

这是jQuery代码。

  $(function(){
 $(document  ).ready(function(e){
 var $ stars = $('。input-star-rate'); 
 
 $ stars.bind('change',function(){
 var $ this =  $(this); 
 
 var ratingValue = $ this.val(); 
 alert(ratingValue); 
 
 var id = $ this.attr(“id”); 
 alert(id);  
 
 var num = parseInt(“$ this.attr(”rate_number“)”)+ 1; 
 alert(num); 
}); 
}); 
}); 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drqvsx1228 2014-11-26 08:55
    已采纳

    You didn't set the id attribute in the <input>.

    $rateid = 0;
    if($jrowa['rate_number'] > 0){ 
      $ratea = $jrowa['rate_score'] / $jrowa['rate_number']; 
      $rateid = $jrowa['id'];
    }else{ 
      $ratea = 0; 
    }
    

    and:

    <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;"/>
    

    Same for num (also doesn't work because you need to escape your quotes)

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题