【问题标题】:how to get the value of rating star with php如何用php获取星级的值
【发布时间】:2021-08-14 18:51:34
【问题描述】:

我正在使用 jquery 星级插件。链接是 (http://irfandurmus.com/projects/jquery-star-rating-plugin/)。我想点击星号,它会给我一个值,所以我可以用 php 将它传递给数据库。 我已经多次破坏插件试图让它工作,但无济于事。它在网站上显示得很好,但我无法将值存储在数据库中。也许我应该修改 js 文件但我不知道如何。这里是:

;(function($){
    $.fn.rating = function(callback){
        
        callback = callback || function(){};

        // each for all item
        this.each(function(i, v){
            
            $(v).data('rating', {callback:callback})
                .bind('init.rating', $.fn.rating.init)
                .bind('set.rating', $.fn.rating.set)
                .bind('hover.rating', $.fn.rating.hover)
                .trigger('init.rating');
        });
    };
    
    $.extend($.fn.rating, {
        init: function(e){
            var el = $(this),
                list = '',
                isChecked = null,
                childs = el.children(),
                i = 0,
                l = childs.length;
            
            for (; i < l; i++) {
                list = list + '<a class="star" title="' + $(childs[i]).val() + '" />';
                if ($(childs[i]).is(':checked')) {
                    isChecked = $(childs[i]).val();
                };
            };
            
            childs.hide();
            
            el
                .append('<div class="stars">' + list + '</div>')
                .trigger('set.rating', isChecked);
            
            $('a', el).bind('click', $.fn.rating.click);            
            el.trigger('hover.rating');
        },
        set: function(e, val) {
            var el = $(this),
                item = $('a', el),
                input = undefined;
            
            if (val) {
                item.removeClass('fullStar');
                
                input = item.filter(function(i){
                    if ($(this).attr('title') == val)
                        return $(this);
                    else
                        return false;
                });
                
                input
                    .addClass('fullStar')
                    .prevAll()
                    .addClass('fullStar');
            }
            
            return;
        },
        hover: function(e){
            var el = $(this),
                stars = $('a', el);
            
            stars.bind('mouseenter', function(e){
                // add tmp class when mouse enter
                $(this)
                    .addClass('tmp_fs')
                    .prevAll()
                    .addClass('tmp_fs');
                
                $(this).nextAll()
                    .addClass('tmp_es');
            });
            
            stars.bind('mouseleave', function(e){
                // remove all tmp class when mouse leave
                $(this)
                    .removeClass('tmp_fs')
                    .prevAll()
                    .removeClass('tmp_fs');
                
                $(this).nextAll()
                    .removeClass('tmp_es');
            });
        },
        click: function(e){
            e.preventDefault();
            var el = $(e.target),
                container = el.parent().parent(),
                inputs = container.children('input'),
                rate = el.attr('title');
                
            matchInput = inputs.filter(function(i){
                if ($(this).val() == rate)
                    return true;
                else
                    return false;
            });
            
            matchInput
                .prop('checked', true)
                .siblings('input').prop('checked', false);
            
            container
                .trigger('set.rating', matchInput.val())
                .data('rating').callback(rate, e);
        }
    });
    
})(jQuery);

mysql数据库存在如下

CREATE TABLE IF NOT EXISTS `rating` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `vote` float NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1

我想用这个php代码来获取星级值但是没办法。

<?php

function connect() {
$hostname = "localhost";
$username = "root";
$password = "root";
$dbname = "rating";
  $con = mysqli_connect($hostname, $username, $password, $dbname);  
  return $con;
}

function getRatingByProductId($con, $productId) {
    $query = "SELECT SUM(vote) as vote, COUNT(vote) as count from rating WHERE product_id = $productId";

      $result = mysqli_query($con, $query);
      $resultSet = mysqli_fetch_assoc($result);
      if($resultSet['count']>0) {
        return ($resultSet['vote']/$resultSet['count']);
      } else {
        return 0;
      }
    
}
if(isset($_REQUEST['type'])) {
    if($_REQUEST['type'] == 'save') {
        $vote = $_REQUEST['vote'];
        $productId = $_REQUEST['productId'];
          $query = "INSERT INTO rating (product_id, vote) VALUES ('$productId', '$vote')";
          // get connection
          $con = connect();
          $result = mysqli_query($con, $query);
          echo 1; exit;
    } 
}

?>

【问题讨论】:

标签: javascript php jquery mysql


【解决方案1】:

您可以使用下面的 jQuery 代码在数据库中提交星号。

jQuery('.stars .star').click(function(){
    var starValue = $(this).closest('.stars').find('.fullStar').length;
    console.log(starValue);

    // Send starValue value with ajax and store on database
});

【讨论】:

  • 但是如何发送starValue值。我写这个-----> $.ajax({ url: "trygo.php", type: "POST", data:{"myData": starValue}, 我的 trygo.php 显示在这里 mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname) or die(mysql_error()); // 从查询字符串中检索数据 $myData = $_GET[' myData']; 是吗?我出错了,myData 变量无法定义
  • 在控制台上,当您点击任何星星时,会打印选定的星星值?
  • 是的,你是对的。我从控制台看到了值1,2,3,4,5
  • 但我不能通过starValue .$.ajax({ url: "trygo.php", type: "POST", data:{"myData":starValue}, url : "trygo. php", type : "POST", //dataType : 'json', data : { 'starValue':starValue, }, 尝试失败
  • 您正在访问错误的参数。在您的 ajax 代码上,您发送的是 starValue 键,而在 php 上,您使用 $_GET['myData']。你可以尝试使用 $_GET['starValue']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 2012-03-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多