星级评定系统试图将值插入数据库

Star Rating System trying to insert value into db

本文关键字:插入 数据库 系统      更新时间:2023-09-26

嗨,我正在使用Nashio/star评级svg

我已经设置了所有关于星星显示的内容,悬停也工作得很好,但问题是我不能让它取值并将其插入我的数据库,它在文档中说了一些关于回调函数的内容,但它对我不起作用

将星号插入html:

<div class="my-rating"></div>

初始化恒星并使其运行的代码:

<script>
        $(".my-rating").starRating({
            starSize: 45,
            <?php
                if(Login::isLogged(Login::$login_front)) {
                    echo "readOnly: false,";
                } else {
                    echo "readOnly: true,";
                }           
            ?>
            disableAfterRate: false,
            callback: function(currentRating) {
                $rating = currentRating;
                <?php
                      $addRating = $objRating->addRating($client_id, $rating);
                ?>
            }
        });
    </script>

问题不在于php代码试图将评级添加到数据库中,而在于函数根本没有被调用或开始执行和插入,而且当我插入回调部分时,星号甚至没有显示,任何成功完成这项工作的人的任何帮助都将是一个很大的帮助,我期待您的回复。

我已经更新了整个答案。它经过测试,正在运行:

明星出现和用户点击评分的主文件:

<!DOCTYPE html>
<html>
    <head>
        <title>Rating</title>
        <!-- Add the plugin's CSS (make sure of path) -->
        <link rel="stylesheet" type="text/css" href="src/css/star-rating-svg.css">
    </head>
    <body>
    <!-- Display the stars on page -->
    <div class="my-rating"></div>
    <!-- Add jquery.min.js -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <!-- Add plugin's required JS (make sure of paths) -->
    <script src="src/jquery.star-rating-svg.js"></script>
    <!-- Initiate the plugin -->
    <script>
        $(".my-rating").starRating({
            starSize: 25,
            callback: function(currentRating, $el){
                $.post('submit_rating.php', {rating: currentRating});
            }
        });
    </script>
    </body>
</html>

将评级插入数据库的PHP文件(submit_rating.php)应该在同一目录中,或者如果您计划将该文件保留在任何内部目录中,请调整上面$_POST请求中的路径。将该文件保存为submit_rating.php,在该文件中,您将收到$_POST['rating']的评级值;然后,您可以将该值插入到数据库中,如下所示:

$rating = $_POST['rating']; // don't forget to sanitize

然后将$rating插入数据库。我希望这能有所帮助。