php - 试图将 krajee bootstrap 星级评分中的星级评分发送到 mysql 数据库

标签 php jquery mysql

我正在使用以下插件

http://plugins.krajee.com/star-rating

我正在尝试将提交的评级发送到数据库,但没有记录这些值。

require_once $doc_root . '/includes/act_initiate_article_xref.php';

    $rating_value = (@$user_article_xref_row['rating'] > 0) ? $user_article_xref_row['rating'] . ' stars' : 'unrated';
    $article_rating = '<span class="line-sep">Rated:</span> ' . $rating_value;
if ($_SESSION['user_id'] == 1 || $_SESSION['user_id'] == 41)    {
    $rating_value_attr = (isset ($user_article_xref_row['rating'])) ? ' value="' . $user_article_xref_row['rating'] . '"' : '';
    $article_rating .= '<span class="line-sep">Your rating: </span> <input id="input-21d" type="number" class="rating"' . $rating_value_attr . ' data-min=0 data-max=5 step=0.5 data-size="xs">';
}

和 act_initiate_article_xref.php 的代码

<?

/**
 * reads user_article_xref
 */
$this_routine[] = "includes/act_initiate_article_xref.php";

/**
 * no direct access allowed
 */
$doc_root = $_SERVER['DOCUMENT_ROOT'];
require_once $doc_root . '/includes/act_check_valid_access.php';

$table_title = 'user_article_xref';

$user_id = (isset ($_SESSION['user_id'])) ? $_SESSION['user_id'] : 0;

$fields = '`id`, `hits`, `rating`';
$match_where = '`user_id` = ' . $user_id . ' and `article_id` = ' . $article_id;
$article_result = $db->selectByStrings($fields, $table_title, $match_where, null, 1);
#if ($_SESSION['user_id'] == 1) { echo "<br>21. select $fields from $table_title where $match_where<pre>";print_r($_SESSION);echo "</pre>"; }

/**
 * if there is already a record, update it
 * if there isn't already a record, insert it
 * either way, $user_article_xref_row holds details for any ajax rating update
 */
if ($db->getNumRows($article_result) > 0)   {
    $user_article_xref_row = $db->getNextRow($article_result);
    $hits = $user_article_xref_row['hits'] + 1;
    $pairs_array = array ('hits' => $hits);
    $id_where = '`id` = ' . $user_article_xref_row['id'];
    $update_result = $db->updateByArray($table_title, $pairs_array, $id_where);
    $test = 'update';
} else {
    $hits = 1;
    $user_article_xref_row = array ('user_id' => $user_id, 'article_id' => $article_id, 'hits' => $hits);
    $insert_result = $db->insertByArray($table_title, $user_article_xref_row);
    $test = 'insert';
}
#if ($_SESSION['user_id'] == 1) { echo "<br>37. $test<pre>";print_r($user_article_xref_row);echo "</pre>"; }

?>

最佳答案

我知道这篇文章有点旧,但以防万一。在 star-rating.js 中,我在 set.listenClick 函数的 php 文件中添加了一个 ajax 帖子。希望这会有所帮助。

self.listenClick(self.$rating, function(e) {
                if (self.inactive) {
                    return false;
                }
                pos = self.getPosition(e);
                var div_id = self.$element.attr('id');
                self.setStars(pos);
                self.$element.trigger('change').trigger('rating.change', [self.$element.val(), self.$caption.html()]);
                self.starClicked = true;
                var rating = self.$element.val();
                $.ajax({
                type:"POST",
                url:"rating.php",
                data:"div="+div_id+"&rating="+rating,
                success: function(res){
                 console.log("Rating was posted " + div_id + rating);
                                             return res;
                }
            });
            });

关于php - 试图将 krajee bootstrap 星级评分中的星级评分发送到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31404151/

相关文章:

php - 检查目录是否存在,创建目录

javascript - 我如何提交我的 jquery 模态表单

java - 将数据库克隆到 MySQL

PHP:将逗号写入 csv 文件

php - 我在 Laravel 5.5 中实现邮件时遇到此错误----> Illuminate\Support\Manager::createDriver() 缺少参数 1

javascript - 使用 javascript substring() 创建阅读更多链接

jquery - 获取html返回文本

php - 使用 AJAX 每次点击更新信息

php - 删除记录时递减单元格

php - 在 Woocommerce 中创建指向外部路由的链接