【问题标题】:Voting system using PHP, MySQL and jQuery - NOT working (DIAGNOSE)使用 PHP、MySQL 和 jQuery 的投票系统 - 不工作(诊断)
【发布时间】: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,看看会发生什么。

标签: jquery ajax voting


【解决方案1】:

由于您在同一页面中使用 jQuery 和 prototype.js,因此您需要注意如何使用 $ 函数(因为两个框架都声称该名称)。一种选择是将您的主要功能替换为:

jQuery(function($) {
    ...
});

这样您就可以在该块内使用$ 作为jQuery 的别名(但您将无法访问prototype.js)。另一种选择是使用 jQuery 的noConflict。有关如何使用它的更多信息,请查看文档。

【讨论】:

  • 谢谢!删除了prototype.js,它现在就像一个魅力。
【解决方案2】:

你可能会喜欢这个 Jquery Ajax 投票系统

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" language="javascript" src="http://code.jquery.com/jquery-latest.js"></script>  
<script>
$(document).ready(function () {

    $(".voteMe").click(function() {
        var voteId = this.id;
        var upOrDown = voteId.split('_'); 
        $.ajax({
            type: "post",
            url: "<?= base_url();?>/index.php/article/voteMe",
            cache: false,               
            data:'voteId='+upOrDown[0] + '&upOrDown=' +upOrDown[1],
            success: function(response){
                try{
                    if(response=='true'){   
                        var newValue = parseInt($("#"+voteId+'_result').text()) + 1;            
                        $("#"+voteId+'_result').html(newValue);
                    }else{
                        alert('Sorry Unable to update..');
                    }
                }catch(e) {     
                    alert('Exception while request..');
                }       
            },
            error: function(){                      
                alert('Error while request..');
            }
         });
    });

});
</script>
<style>
ul,ol,li{
    list-style-type: none;
}

h2{
    color: #2864B4;
    text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<?php
    if(is_array($VOTES) && count($VOTES) ) {
?>
<div>
    <ul>
    <?php foreach($VOTES as $loop){ ?>               
        <li>
        <div>
            <h2><?=$loop->VOTE_DESC;?></h2>
            <span><a id="<?=$loop->VOTE_ID;?>_upvote" class="voteMe"><img src="<?= base_url();?>/img/images/up_vote.png"/></a><span id="<?=$loop->VOTE_ID;?>_upvote_result" ><?=$loop->UP_VOTE;?></span></span>&nbsp;|&nbsp;
            <a id="<?=$loop->VOTE_ID;?>_downvote" class="voteMe"><img src="<?= base_url();?>/img/images/down_vote.png"/></a><span id="<?=$loop->VOTE_ID;?>_downvote_result" ><?=$loop->DOWN_VOTE;?></span>
        </div>
        </li>
    <?php }?>   
    </ul>
</div>
<?php }?>

</div>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2017-03-22
    相关资源
    最近更新 更多