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条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决