一个带有jQuery,PHP和Smarty的投票系统 [英] A voting system with jQuery, PHP and Smarty

查看:94
本文介绍了一个带有jQuery,PHP和Smarty的投票系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只用PHP和Smarty在我的网站上投了一个投票功能。
这是HTML的一部分:

 < 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为H. 

代码的PHP部分需要投票并刷新同一页面。

我想用jQuery做同样的事情,这样就不需要刷新页面。这是我在HTML中写的内容:

  $(#q_upvote)。click(function()
{
var vote =vote_up;
var votedata =;
votedata =vote =+ vote;
$ .ajax({
type: 'POST',
url:'vote.php',
data:votedata,
成功:function(vote_msg){
if(msg =='ok')
{
//显示新的投票
}
else
//显示通知
}
}

< / script>

我不知道如何在那里显示新的投票。如果我被告知,如果我正确的做法。

解决方案


  • 更正了哟您的html代码,以便您有
    票数的占位符。

  • 修正Ajax调用,以便它传递相同的参数
    您的初始upvote锚定器中的hrefs。
  • 修复了ajax调用中的各种语法错误



HTML代码

 < p为H. 
< span class =votenumbers> {$ vote}< / span>
< a id =upvote _ {$ qid}class =q_upvotehref =#>< i class =icon-thumbs-up>< / i><一个>
< a href =vote.php?q_vote = vote_down& question_id = {$ qid}>< i class =icon-thumbs-down< / i< / a>
< ; / p>

jQuery代码

  $(。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,
成功:function(vote_msg){
if(vote_msg =='ok')
{
//显示新投票
$(this).find('。votenumbers')。closest()。html(parseInt($(this).find('。votenumbers' ).closest()。html())+ 1)
}
else {
//显示通知
}
}
});
}


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.

解决方案

  • 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
                   }
                }
           });
    }
)

这篇关于一个带有jQuery,PHP和Smarty的投票系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆