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

报告相同问题?

悬赏问题

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