好吧所以我的主要问题是,即时通讯尝试发布我的php文件,其中包含mysqli_query以插入关注者并删除关注者,我已经通过制作<a href="follow.php?action=follow&uid=$userid">follow</a>
以简单的方式尝试了它通过跟随和取消关注工作得很好,但现在我试图在不刷新页面的情况下这样做,但不知何故它根本不工作。我是jquery的新手:)。
<?php
$query = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT id FROM follow_user WHERE userid='$userid' AND follow_id='$uid'");
if(mysqli_num_rows($query)== 1)
{
echo "<div id='follow$userid' style='display:none'><a href='' class='follow' id='$userid'><span class='btn'style='width:70px;'><b> Follow </b></span></a></div>";
echo"<div id='remove$userid' ><a href='#' class='remove' id='$userid'><span class='btn btn-info' style='width:70px;'><b> Following </b></span></a></div>";
}
else
{
echo "<div id='follow$userid'><a href='' class='follow' id='$userid'><span class='btn'style='width:70px;'><b> Follow </b></span></a></div>";
echo"<div id='remove$userid' style='display:none'><a href='#' class='remove' id='$userid'><span class='btn btn-info' style='width:70px;'><b> Following </b></span></a></div>";
}
?>
jquery
$(function () {
$(".follow").click(function () {
var datastring = '?action=follow&uid=' + userid;
$.ajax({
type: "POST",
url: "include/follow_user.php",
data: datastring,
success: function (html) {}
});
$("#follow" + userid).hide();
$("#remove" + userid).show();
alert("Yeah!");
return false;
});
});
//remove class
$(function () {
$(".remove").click(function () {
var datastring = '?action=unfollow&uid=' + userid;
$.ajax({
type: "POST",
url: "include/follow_user.php",
data: datastring,
success: function (html) {}
});
$("#remove" + userid).hide();
$("#follow" + userid).show();
alert("Yeah!");
return false;
});
});
答案 0 :(得分:1)
成功操作的代码应该在内部 success
回调中,否则它们将随时被调用而不会得到请求本身的结论。看看:
$(".follow").click(function () {
$.ajax({
type: "POST",
url: "include/follow_user.php",
data: {action:"follow", uid: userid},
success: function (html) {
$("#follow" + userid).hide();
$("#remove" + userid).show();
alert("Yeah!");
}
});
});
然后在你的&#34; follow_user.php&#34;你必须返回进程状态(例如1
ok - 2
错误):
if(mysqli_num_rows($query)== 1)
{
echo "1";
}
else
{
echo "2";
}
然后在你的回调中你检查它:
success: function(data)
{
if (data == "1")
{
$("#follow" + userid).hide();
$("#remove" + userid).show();
alert("Yeah!");
}
else
{
alert("Some error occur");
}
}