reddit的风格投票按钮 [英] Reddit-Style Voting Button

查看:107
本文介绍了reddit的风格投票按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的网站上reddit的风格的投票按钮。我有它的工作很好,唯一的问题是,用户可以投票无限量的时间目前。我需要它来运行SQL查询和检查数据库,看看他们是否接受在投票之前已经投票。

问题是:我在哪里可以把一个SQL查询到这个AJAX POST禁用的按钮,如果投票的存在?这是我到目前为止有:

  $(函数(){
    $(a.vote_up)。点击(函数(){
    //获取ID



    the_id = $(本).attr('身份证');

    //显示微调
    $(本).parent()追加(< D​​IV ID ='spinnerDiv'风格='宽度:20像素;高度:20像素;浮动:权利;保证金右:570px;'>< IMG SRC ='图片/ spinner.gif/>< / DIV>中);

    //淡出的投票计数
    $(#跨度votes_count,跨度#vote_buttons+ the_id).fadeOut(快);


    //主ajax请求
        $阿贾克斯({
            键入:POST,
            数据:行动= vote_up和ID =+ $(本).attr(ID),
            网址:?????votes.php PERSONID =< PHP的echo $ PERSONID;>&安培;用户id =< PHP的echo $用户ID;>中,
            成功:函数(MSG)
            {

                $(#跨度votes_count+ the_id)的.html(MSG);
                //淡入计票
                $(#跨度votes_count+ the_id).fadeIn();
                //删除微调
                $(#spinnerDiv)删除()。
                $(#跨度vote_buttons+ the_id).fadeIn();

            }
        });

    });
 

解决方案

您不想把一个SQL查询在那里,这不是AJAX。你应该叫一个URL处理器具有code来执行查询和结果返回JSON。因此,在votes.php你的PHP应用程序应该做这种检查,并返回一个错误,如果人已经投票。然后,你可以设置一个div,将闪烁用户已经投票。

因此​​,例如,你的应用程序可以返回JSON: {成功:假消息:用户已经投票}

请参阅: http://api.jquery.com/jQuery.post/ 如何检索JSON

修改

下面是工作流我所说的:

在index.php文件有jQuery的像你现在有,而且职位votes.php的用户ID和他们在投票上。

在votes.php你再得到这些参数。在投票PHP的逻辑是这样的:

  $查询= sprintf的(SELECT * FROM票的uid ='%s'和vote_id ='%s'的,
mysql_real_escape_string($ UID),
mysql_real_escape_string($ vot​​e_id));

如果(的mysql_query($查询)){
   返回json_en code(阵列(状态=>失败,消息=>中的用户已经投);
}
其他{
   do_vote($ UID,$ vot​​e_id);
   返回json_en code(阵列(状态=>中成功));
}
 

然后在你的AJAX功能,你可以去$ C C此$。如果你获得成功,变灰按钮。如果你得到错误已经投票,不计票,变灰按钮。如果你没有反应,离开按钮,并告诉用户出现了错误。

I am trying to implement a reddit-style voting button on my site. I have it working well, the only problem is that users can vote an unlimited amount of times currently. I need it to run a SQL query and check the database to see if they have voted before accepting the vote.

Question is: Where can I put a SQL query into this AJAX POST that disables the button if a vote exists? Here is what I have so far:

$(function(){
    $("a.vote_up").click(function(){
    //get the id



    the_id = $(this).attr('id');

    // show the spinner
    $(this).parent().append("<div id='spinnerDiv' style='width:20px; height:20px; float:right; margin-right:570px;'><img src='images/spinner.gif'/></div>");

    //fadeout the vote-count 
    $("span#votes_count, span#vote_buttons"+the_id).fadeOut("fast");


    //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_up&id="+$(this).attr("id"),
            url: "votes.php?personid=<?php echo $personid;?>&userid=<?php echo $userid;?>",
            success: function(msg)
            {   

                $("span#votes_count"+the_id).html(msg);
                //fadein the vote count
                $("span#votes_count"+the_id).fadeIn();
                //remove the spinner
                $("#spinnerDiv").remove();
                $("span#vote_buttons"+the_id).fadeIn();

            }
        });

    });

解决方案

You do not want to put an SQL query in there, that is not AJAX. You should call a url handler that has code to perform the query and return json with the result. So your PHP application at votes.php should do this check and return an error if the person has already voted. Then you can set a div that will flash that user has already voted.

So for instance your app can return json: {successful : false, message: "user has already voted"}

see: http://api.jquery.com/jQuery.post/ on how to retrieve that json

EDIT

Here is the workflow I am talking about:

In index.php you have jquery like you have now, and it POSTs to votes.php the user id and what they are voting on.

In votes.php you then get those parameters. The logic in votes php would be something like:

$query = sprintf("SELECT * from votes uid='%s' AND vote_id='%s'",
mysql_real_escape_string($uid),
mysql_real_escape_string($vote_id));

if(mysql_query($query)){
   return json_encode(array("status" => 'failure', "message" => "user already voted");
}
else{
   do_vote($uid,$vote_id);
   return json_encode(array("status" => "success"));
}

Then in your ajax function you can decode this. If you get success, grey out button. If you get error already voted, dont count vote, and grey out button. If you get no response, leave button and tell the user there was an error.

这篇关于reddit的风格投票按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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