繁体   English   中英

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

[英]After button clicked, save to MySQL then display CSS highlight

我有一个Rating函数,该函数的作用如下:用户单击+1按钮后,评分上升1,并保存到MySQL。 我想做的是单击一下,它将背景更改为另一种颜色,如下所示。

(我现在想做的是“单击一次”),它只是更新背景为白色的数字)

注意:我只是在寻求建议或以某种方式引导我朝正确的方向前进,在此先感谢您。

无需点击:

在此处输入图片说明

点击后:

在此处输入图片说明

php / html表单:提交+1

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

AJAX:更新按钮

$(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:从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'];
}

在您的成功回调中,为什么不只是将一个类设置parent类,然后更新.wrapper

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

当用户刷新页面并且您想要继续显示蓝色时,您只需:

<?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>

首先,您不能在类名称中带有空格-它会被解释为两个类,分别是up表决

在php中,您可以沿的方式回显某些内容(请确保将dataType设置为json)

echo(json_encode(array("success"=>true)));
exit();

之后,jQuery可以处理响应:

function(result) {
    var result_string = jQuery.parseJSON(result);
    if(result_string.success) {
        $(this).children(".wrapper").css("background-color", "blue")
    }
}

您可以更改成功函数,以使用.css(“ background-color”,“ blue”)添加颜色,或者如果钢笔投票数高于您想要的颜色,则始终将其设置为蓝色,可以将其添加到代码顶部:

if (parseInt($("#"+id).children(".wrapper").text()) >= 1) {
 $("#"+id).children(".wrapper").css("background-color","blue");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM