我想在点击按钮时创建一个关注/取消关注用户。但是,当前代码仅在页面刷新之前有效。如果我刷新页面,则会返回以下文本。我希望它在刷新页面时取消关注。
我的PHP中是否需要为用户设置会话?我是否必须为按钮创建值?它会记住多个用户的点击吗?
<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会话的用户。 我怎样才能做到这一点?
答案 0 :(得分:0)
我假设您正在更新&#34; UnFollow.php&#34;你应该在&#34; echocurrent.php&#34;中返回这个值。并基于它显示/隐藏班级&#34;用户&#34;
更新: 将逻辑添加到&#34; echocurrent.php&#34;返回关注者数量以及当前用户是否关注此用户
$.ajax({
type: 'POST',
url: 'echocurrent.php',
success: function(data) {
$("p").text(data.followers);
if(data.isFollowing){
$this.toggleClass('user');
}
}
});