简体   繁体   English

星级评分系统保存值单击仅保存在数据库中的值

[英]Star rating system save value on click only value save in database

I'm trying to make a star rating system, i have this code and i want to make some changes on it, after the user clicks on the stars it shows an alert with how many stars and it resets the colors, what i want is the color filling to stay after the user clicks on it, and replace the alert with a div under the stars, here is my code:我正在尝试制作星级评分系统,我有此代码,我想对其进行一些更改,在用户单击星星后,它会显示带有多少星星的警报并重置颜色,我想要的是用户点击后保留的颜色填充,并用星星下的 div 替换警报,这是我的代码:

Js: JS:

$(function() {
    $("div.star-rating > s, div.star-rating-rtl > s").on("click", function(e) {

    // remove all active classes first, needed if user clicks multiple times
    $(this).closest('div').find('.active').removeClass('active');

    $(e.target).parentsUntil("div").addClass('active'); // all elements up from the clicked one excluding self
    $(e.target).addClass('active');  // the element user has clicked on


        var numStars = $(e.target).parentsUntil("div").length+1;
        $('.show-result').text(numStars + (numStars == 1 ? " star" : " stars!"));
    });
});

CSS: CSS:

.show-result {
  margin: 10px;
  padding: 10px;
  color: green;
  font-size: 20px;
}

.star-rating s:hover,
.star-rating s.active {
    color: red;
}
.star-rating-rtl s:hover,
.star-rating-rtl s.active {
    color: red;
}

.star-rating s,
.star-rating-rtl s {
    color: black;
    font-size: 50px;
    cursor: default;
    text-decoration: none;
    line-height: 50px;
}
.star-rating {
    padding: 2px;
}
.star-rating-rtl {
    background: #555;
    display: inline-block;
    border: 2px solid #444;
}
.star-rating-rtl s {
    color: yellow;
}
.star-rating s:hover:before,
.star-rating s.rated:before,
.star-rating s.active:before {
    content: "\2605";
}
.star-rating s:before {
    content: "\2606";
}
.star-rating-rtl s:hover:after,
.star-rating-rtl s.rated:after,
.star-rating-rtl s.active:after {
    content: "\2605";
}

.star-rating-rtl s:after {
    content: "\2606";
}

html html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="star-rating"><s><s><s><s><s></s></s></s></s></s></div>
<div class="show-result">No stars selected yet.</div>

here value can display but how to store this value in mysql.这里的值可以显示但是如何在mysql中存储这个值。

To store the star ratings in database, you should use Ajax.要将星级评分存储在数据库中,您应该使用 Ajax。 The code below shows using Jquery Ajax post to post values to a server side script (PHP).下面的代码显示了使用 Jquery Ajax post 将值发布到服务器端脚本 (PHP)。

$(e.target).addClass('active');  // the element user has clicked on

var numStars = $(e.target).parentsUntil("div").length+1;
// modification starts here....
$.post( "saveRatings.php", { rating: numStars})// you may pass other necessary information 
     .done(function( data ) {
          $('.show-result').text(numStars + (numStars == 1 ? " star" : " stars!"));
});

on your saveRatings.php file,在您的saveRatings.php文件中,

$ratings = $_POST['ratings'];
// You may need to pass more information to identify the user who gives the rating or the product that is being rated.

// Add your MySQL query to save the rating information to a database table

// return a success/failure response as needed.

You may need to fetch the rating from the database and pass it to the javascript code to persist the ratings across page reloads.您可能需要从数据库中获取评级并将其传递给 javascript 代码以在页面重新加载时保留评级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM