我正在使用jQuery插件来获得我在PHP驱动的网站上发布的每篇帖子的评分。
我使用的插件名为Raty,可以找到here。
我想知道如何保存此评级,因为我可以点击星标然后获得5星评级,但如果我刷新页面,评分就会消失。
所以我应该以某种方式保存它。
答案 0 :(得分:3)
您可以针对点击事件尝试以下操作:(示例代码)
$('#click').raty({
click: function(score, evt) {
$.ajax({
type: 'POST',
url: '/SaveMyRating.php',
data: {'score':score},
success: function(data){ alert('Your rating was saved'); }
});
}
});
SaveMyRating.php应该是php脚本,它将接收提交的分数并保存(文件或数据库等)。
答案 1 :(得分:2)
首先,您应该在数据库中为此创建一个表。
点击星标后,将费率信息(通过使用Ajax)发送到服务器计算机并将其插入表格中。
答案 2 :(得分:2)
您可以在此处使用ajax将最新评级(整数)以及其他必要的详细信息发送到php页面。然后将评级保存到数据库中。
// rate.php - the php page where you will insert rating.
$('#rate').raty({
click: function(score) {
var id = // get the id of the object for which the rating is done
$.post('rate.php', {score:score,id:id}, function(data) {
// data is a variable that may or may not be
returned from the rate.php page
});
}
});