[英]Follow/unfollow to remember the click of the button. Not working after refresh
我想在按钮单击上设置关注/取消关注用户。 但是,当前代码仅在页面刷新之前有效。 如果我刷新页面,则会返回以下文本。 我希望在刷新页面时取消关注。
我是否需要在PHP中为用户提供一个Session? 我必须为按钮创建值吗? 它会记住多个用户的点击吗?
<button class="user">Follow</button>
<p></p>
$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'echocurrent.php',
success: function(data) {
$("p").text(data);
}
});
});
$('.user').click(function() {
var $this = $(this);
$this.toggleClass('user');
if ($this.hasClass('user')) {
//unfollows
$.ajax({
type: 'POST',
url: 'UnFollow.php',
success: function(data) {
$("p").text(data);
}
});
$this.text('Follow');
} else {
//follows
$.ajax({
type: 'POST',
url: 'follow.php',
success: function(data) {
$("p").text(data);
}
});
$this.text('UnFollow');
}
});
更新:
我想将此jquery添加到每个在PHP中具有会话的用户。 我怎样才能做到这一点?
我假设您要更新“ UnFollow.php”中的值,则应在“ echocurrent.php”中返回此值,并基于它显示/隐藏类“ user”
更新:在“ echocurrent.php”中添加逻辑以返回关注者数量以及当前用户是否关注此用户
$.ajax({
type: 'POST',
url: 'echocurrent.php',
success: function(data) {
$("p").text(data.followers);
if(data.isFollowing){
$this.toggleClass('user');
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.