我喜欢按钮的默认颜色是灰色,当用户像按钮一样击中它将运行togglepost“喜欢”,颜色将变成红色..我使用Ajax插入数据,如果用户点击按钮...并且如果用户再次点击类似按钮时,它的红色将运行togglepost“不喜欢”,从我的数据库中的数据将被删除..如何保持按钮的颜色,当点击和刷新页面
按钮等
jQuery函数
<script>
$(".like").click(function(){
$('.boxcoracao .coracao',this).toggleClass("ativo");
var lpid = $(this).closest("div#buttons").find("#likepid").val();
var lmid = $(this).closest("div#buttons").find("#likemid").val();
if ($('.boxcoracao .coracao',this).hasClass("ativo")){
// update the text to show what the next click would be
togglePost("like", lpid, lmid); // run function
alert("succes");
} else {
// update the text to show what the next click would be
togglePost("dislike", lpid, lmid); // run function
alert("failed");
}
function togglePost(action,lpid,lmid){
$.ajax({
type: "post",
url: "../controller/like_controller.php",
data: "action="+action+"&postid="+lpid+"&postmember="+lmid,
success: function(data){
window.location.reload();
},
error: function(e){
alert("please try again...");
}
});
}
});
</script>
这里是我的sql查询 ....
function UpdateLikes($postid, $postmember, $likeid, $action){
if ($action == "dislike"){
$sql = "DELETE FROM plike WHERE pl_puid = '$postid' AND pl_uid = '$likeid'";
var_dump($sql);
$result = $this->dbh->prepare($sql);
$result->execute();
return $result->rowCount() ? true : false;
} else{
// before inserting you might want to check if they alredy liked or not before adding their count again.
$query = "INSERT INTO plike SET pl_puid='$postid',pl_memid='$postmember',pl_uid='$likeid'";
$query = $this->dbh->prepare($query);
var_dump($query);
$result = ($query->execute() ? true : false);
return $result;
}
}
这里是GIF按钮..
但我的问题是当用户打像按钮和刷新页面像按钮将重置为灰色,而不是红色..任何想法如何解决这个问题?..当用户点击时,它会是红色和当用户r efresh该页面的按钮仍然是红色的,除非用户再次点击该按钮以不同的方式...
。你确定你这么做吗?因为我没有看到你发布的代码中发生过这种情况。 – Rehan
是的先生....我这样做我有ajax保存数据到我的数据库我只是没有包括它在我的文章,但喜欢和不同的是保存在我的数据库..我唯一的问题是当我刷新/重新加载页面按钮变灰,即使我已经打了像按钮.. –
如果是这样的话,那么你一定是做错了'ajax'告诉我们'ajax'来帮助你更好。 – Rehan