drxkx6149 2013-05-10 12:17
浏览 86
已采纳

使用jQuery,PHP和Smarty的投票系统

I have made a voting feature to my website using only PHP and Smarty. This's the HTML part of it:

<p>{$vote} <a href="vote.php?q_vote=vote_up&question_id={$qid}"><i class="icon-thumbs-up"></i></a> <a href="vote.php?q_vote=vote_down&question_id={$qid}"><i class="icon-thumbs-down"></i></a></p>

The PHP part of the code takes the vote and refreshes the same page.

I want to do the same thing using jQuery so that it won't need to refresh the page. Here is what I wrote in the HTML :

$("#q_upvote").click(function()
    {
        var vote = "vote_up";
        var votedata = "";
        votedata = "vote= " + vote;
        $.ajax({                 
                type: 'POST',
                url: 'vote.php',
                data: votedata,
                success: function(vote_msg){
                if(msg == 'ok')
                    {
                    //show the new vote 
                    }
                                        else
                                        //show notification
                }
    }
)
</script>

I couldn't figure how to show the new vote there. Can you help me with that? Also I appreciate if I'm told that if I'm going on the right way.

  • 写回答

2条回答 默认 最新

  • douxiangbiao1899 2013-05-10 12:37
    关注
    • Corrected your html code so that you have a placeholder for the number of votes.

    • Corrected the Ajax call so that it passes the same parameters as per the hrefs in your initial upvote anchor.

    • Fixed various syntax errors in the ajax call

    Html Code

        <p>
           <span class="votenumbers">{$vote}</span> 
           <a id="upvote_{$qid}" class="q_upvote" href="#"><i class="icon-thumbs-up"></i></a> 
           <a href="vote.php?q_vote=vote_down&question_id={$qid}"><i class="icon-thumbs-down</i</a>
       </p>
    

    jQuery Code

    $(".q_upvote").click(function()
        {
            var vote = "vote_up",
                question_id = this.id.split('_')[1], 
                votedata = "q_vote="+vote+"&question_id="+question_id;
            $.ajax({                 
                    type: 'POST',
                    url: 'vote.php',
                    data: votedata,
                    success: function(vote_msg){
                       if(vote_msg== 'ok')
                           {
                           //show the new vote
                           $(this).find('.votenumbers').closest().html(parseInt($(this).find('.votenumbers').closest().html())+1) 
                           }
                       else{
                            //show notification
                       }
                    }
               });
        }
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?