dongzaotiao2863 2013-07-03 20:23
浏览 48
已采纳

单击按钮后,保存到MySQL然后显示CSS高亮显示

I've got a Rating function that does as follows: Once a user clicks the +1 button, the rating goes up by 1, and saves to MySQL. What I'd like for it to do is once clicked, it changes the background to a different color as shown below..

( What I'd like for it to do is the "once clicked" ) at the current moment it just updates the number with the background being white)

NOTE: I'm just asking for suggestions or some way to lead me in the right direction, thank you in advance.

Without being clicked:

enter image description here

Once clicked:

enter image description here

php/html form: to submit the +1

<div class="up vote" name="voteUp" id="<?php echo $post_iD;?>">
    <div class="wrapper">+<?php echo $VoteRate;?></div>
</div>

AJAX: to update the button

$(function()
{
    $(".vote").click(function()
    {
        var id = $(this).attr("id");
        var name = $(this).attr("name");
        var dataString = 'id='+ id ;
        var parent = $(this);

        if (name=='voteUp')
        {
            $.ajax(
            {
                type: "POST",
                url: "voting/up_vote.php",
                data: dataString,
                cache: false,
                success: function(html)
                {
                    parent.html(html);
                }
            });
        }
        return false;
    });
});

up_vote.php: submit from the ajax

$ip = $_SERVER['REMOTE_ADDR']; 
if($_POST['id'])
{
    $sth = $db->prepare("SELECT add_iP FROM PostsRating WHERE post_iD_fk = :id AND add_iP = :ip");
    $sth->execute(array(':id' => $_POST['id'], ':ip' => $ip));

    if( $sth->fetchColumn() == 0)
    {
            $sth = $db->prepare("UPDATE posts set voteUp = voteUp+1 where post_iD = :id");
            $sth->execute(array(':id' => $_POST['id']));

            $sth = $db->prepare("INSERT into PostsRating (post_iD_fk, add_iP) VALUES (:id, :ip)");
            $sth->execute(array(':id' => $_POST['id'], ':ip' => $ip));
    } else  {
            $sth = $db->prepare("UPDATE posts set voteUp = voteUp-1 where post_iD = :id");
            $sth->execute(array(':id' => $_POST['id']));

            $sth = $db->prepare("DELETE FROM PostsRating WHERE post_iD_fk = :id AND add_iP = :ip");
            $sth->execute(array(':id' => $_POST['id'], ':ip' => $ip));
    }

    $sth = $db->prepare("SELECT voteUp FROM posts WHERE post_iD = :id");
    $sth->execute(array(':id' => $_POST['id']));

    $row = $sth->fetch();   

    echo $row['voteUp'];
}
  • 写回答

3条回答 默认 最新

  • doudou1309 2013-07-03 20:32
    关注

    In your success callback, why not just set a class to the parent and then update the .wrapper?

    success: function(html)
    {
        parent.addClass("blue");
        parent.find(".wrapper").html("+ " + html);
    }
    

    When the user refreshes the page and you want to continue to show the blue, you would simply:

    <?php
    $ip = $_SERVER['REMOTE_ADDR']; 
    $sth = $db->prepare("SELECT add_iP FROM PostsRating WHERE post_iD_fk = :id AND add_iP = :ip");
    $sth->execute(array(':id' => $post_iD, ':ip' => $ip));
    $class = ($sth->fetchColumn()) ? " blue" : "";
    ?>
    <div class="up vote<?php echo $class; ?>" name="voteUp" id="<?php echo $post_iD;?>">
        <div class="wrapper">+<?php echo $VoteRate;?></div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?