我想在我的网站上实现一个投资分析风格的投票按钮。我有它运作良好,唯一的问题是用户目前可以投票无限次。我需要它来运行一个SQL查询并检查数据库,看看他们在接受投票前是否投了票。Reddit式的投票按钮
问题是:我在哪里可以将SQL查询放入此AJAX POST中,如果存在投票则禁用该按钮?这是我到目前为止:
$(function(){
$("a.vote_up").click(function(){
//get the id
the_id = $(this).attr('id');
// show the spinner
$(this).parent().append("<div id='spinnerDiv' style='width:20px; height:20px; float:right; margin-right:570px;'><img src='images/spinner.gif'/></div>");
//fadeout the vote-count
$("span#votes_count, span#vote_buttons"+the_id).fadeOut("fast");
//the main ajax request
$.ajax({
type: "POST",
data: "action=vote_up&id="+$(this).attr("id"),
url: "votes.php?personid=<?php echo $personid;?>&userid=<?php echo $userid;?>",
success: function(msg)
{
$("span#votes_count"+the_id).html(msg);
//fadein the vote count
$("span#votes_count"+the_id).fadeIn();
//remove the spinner
$("#spinnerDiv").remove();
$("span#vote_buttons"+the_id).fadeIn();
}
});
});
感谢您的回复。我仍然对如何将JSON返回给index.php感到困惑。如果PHP在vote.php POST一个布尔值(不投票或不),然后在index.php上使用JSON来获取该布尔值的结果? – user547794 2011-02-12 18:56:52
你的index.php应该发布用户ID和他们投票的内容。然后在vote.php中的代码应该看看他们是否投了票。如果他们已经投票了,像我说的那样返回一个JSON散列。在AJAX中,JavaScript发起对某个URL的请求(如votes.php)并异步获取响应。我会为你更新我的答案 – 2011-02-12 20:55:08