【发布时间】:2012-05-26 01:10:49
【问题描述】:
试图让这个Reddit-style Voting With PHP, MySQL And jQuery工作。
问题:当我点击“投票赞成”或“投票反对”时,它没有返回任何内容。页面上什么也没有发生。
我已经确定 votes.php 页面虽然可以正常工作,但无法弄清楚为什么其余页面没有。
基本上有3组代码:
01.进入head>的jQuery脚本,
02.里面的html body>(显示:投票按钮和结果)/index.php...
03. jquery 将操作发送到的 PHP 页面 (votes.php)。
01.) 放在我的head>:
<script type="text/javascript" src="http://site.com/js/jquery.pack.js"></script>
<script type="text/javascript">
$(function(){
$("a.vote_up").click(function(){
//get the id
the_id = $(this).attr('id');
// show the spinner
$(this).parent().html("<img src='images/spinner.gif'/>");
//fadeout the vote-count
$("span#votes_count"+the_id).fadeOut("fast");
//the main ajax request
$.ajax({
type: "POST",
data: "action=vote_up&id="+$(this).attr("id"),
url: "votes.php",
success: function(msg)
{
$("span#votes_count"+the_id).html(msg);
//fadein the vote count
$("span#votes_count"+the_id).fadeIn();
//remove the spinner
$("span#vote_buttons"+the_id).remove();
}
});
});
$("a.vote_down").click(function(){
//get the id
the_id = $(this).attr('id');
// show the spinner
$(this).parent().html("<img src='images/spinner.gif'/>");
//the main ajax request
$.ajax({
type: "POST",
data: "action=vote_down&id="+$(this).attr("id"),
url: "votes.php",
success: function(msg)
{
$("span#votes_count"+the_id).fadeOut().html(msg).fadeIn();
$("span#vote_buttons"+the_id).remove();
}
});
});
});
</script>
02.) 放置在 body> 内的循环中:
(投票按钮和结果应该显示在哪里)
<div class=\"entry\">
<span class=\"votes_count\" id=\"votes_count" .$id. "\">" .$effective_vote. " votes</span>
<span class=\"vote_buttons\" id=\"vote_buttons" .$id. "\">
<a href=\"javascript:;\" class=\"vote_up\" id=\"" .$id. "\">Vote Up!</a>
<a href=\"javascript:;\" class=\"vote_down\" id=\"" .$id. "\">Vote Down!</a>
</span>
</div>
03.) 我的votes.php 页面(操作发生的地方):
// Database connection here //
function getAllVotes($id)
{
$votes = array();
$q = "SELECT * FROM cover WHERE id='$id' ";
$r = mysql_query($q) or die("Error: ". mysql_error(). " with query ". $q);
if(mysql_num_rows($r)==1) {
$row = mysql_fetch_assoc($r);
$votes[0] = $row['votes_up'];
$votes[1] = $row['votes_down'];
}
return $votes;
}
function getEffectiveVotes($id)
{
/**
Returns an integer
**/
$votes = getAllVotes($id);
$effectiveVote = $votes[0] - $votes[1];
return $effectiveVote;
}
$id = $_POST['id'];
$action = $_POST['action'];
//get the current votes
$cur_votes = getAllVotes($id);
//ok, now update the votes
if($action=='vote_up') //voting up
{
$votes_up = $cur_votes[0]+1;
$q = "UPDATE cover SET votes_up = $votes_up WHERE id = $id";
}
elseif($action=='vote_down') //voting down
{
$votes_down = $cur_votes[1]+1;
$q = "UPDATE cover SET votes_down = $votes_down WHERE id = $id";
}
$r = mysql_query($q);
if($r) //voting done
{
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote." votes";
}
elseif(!$r) //voting failed
{
echo "Failed!";
}
具体来说,与在线提供的其他评分方法相比,我特别喜欢这种方法,它返回总票数(从赞成票中减去反对票数,并返回该值)。
我不会做很多 jQuery 或 ajax,它有时会让我感到困惑,但我有非常基本的简单知识。 (我通常只是遵循代码)
+ 另外,我如何将它变成 PDO 版本,而不是使用 MySQL 查询?如果有人可以对此提供额外帮助,我将不胜感激。 :o)
不知道如何在 StackOverflow 上通知用户,但这个项目的开发者是:
用户:abhisek(用户:4507330)
感谢您的宝贵时间!希望有人能诊断出问题的根源。
【问题讨论】:
-
请尝试用一个代码示例和一个答案提出一个问题。
-
我打赌你的 ajax 调用正在浏览器中缓存。
-
@asawyer:POST 请求永远不会被缓存。
-
@Love 在 Chrome 上,按 F12 调出开发者工具。在那里你会找到
Console -
@Love 你有没有机会在同一页面中除了 jQuery 之外还有其他框架? (例如prototype.js)
$(something)应该永远是null。我看到 main 函数正在被调用,或者代码不会到达那个点来引发该错误。尝试明确地使用jQuery.click而不是$.click,看看会发生什么。