Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何用php获取评级星的值_Javascript_Php_Jquery_Mysql - Fatal编程技术网

Javascript 如何用php获取评级星的值

Javascript 如何用php获取评级星的值,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,我正在使用jquery星级插件。链接是()。我想点击星号,它会给我一个值,所以我可以用php将它传递到数据库。 为了让插件正常工作,我多次破坏了它,但都没有用。它在网站上显示得很好,但我无法将值存储在数据库中。也许我应该修改js文件,但我不知道如何修改。这里是: ;(function($){ $.fn.rating = function(callback){ callback = callback || function(){}; //

我正在使用jquery星级插件。链接是()。我想点击星号,它会给我一个值,所以我可以用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);
我想用这段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;
    } 
}

?>

您可以使用下面的jQuery代码提交数据库上的star

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
});

警告:您完全可以使用参数化的预处理语句,而不是手动生成查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行。但是如何发送饥饿值。我写这个------->$.ajax({url:“trygo.php”,键入:“POST”,data:{“myData”:饥饿值},我的trygo.php显示在这里mysql\u connect($dbhost,$dbuser,$dbpass);mysql\u select\u db($dbname)或die(mysql\u error());//从查询字符串中检索数据$myData=$\u GET['myData'];是吗?我得到了erro并且myData变量无法在控制台上定义当你点击任何星型时是否有打印选中的星型值?是的,你是对的。我从控制台1,2,3,4,5看到了值,但我无法传递饥饿值。$.ajax({url:“trygo.php”,键入:“POST”,data:{“myData”:饥饿值},url:“trygo.php”,键入:“POST”,//数据类型:'json',数据:{'饥饿值':饥饿值,},try并失败您访问了错误的参数。在ajax代码上发送饥饿值键,在php上使用$\u GET['myData']。您能试着使用$\u GET['饥饿值']
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
});