我想实现的是一个用户跟随另一个用户而不需要刷新页面。到目前为止,我已经玩过了,在mysql表中插入和删除行没有问题,但现在当我尝试使用AJAX时,我不能让它工作。
查询
//the button for following and unfollowing a user
$(document).ready(function(){
$("#followbutton").click(function(e) {
e.preventDefault();
var theuserid = $('#theuserid').val();
var thefollower = $('#thefollower').val();
$.ajax({
url: 'includes/followuser.inc.php',
type: 'post',
data: {'theuserid': theuserid, 'thefollower': thefollower, 'followbutton': true},
success: function(response){
$('#followmessage').html(response);
$("#followmessage").show().delay(3000).fadeOut();
//I want the button to change its text to Following and when hovering it should say unfollow if user is followed
$('#followbutton').text("Unfollow");
}
});
});
});
字符串
followuser.inc.php
<?php
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
if (isset($_POST["submitFollow"])){
$userthatisfollowed = $_POST["thefollower"];
$theuserid = $_POST["theuserid"];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare('INSERT INTO userfollow (thefollower, theuserid, followstatus) VALUES (?,?,?)');
$followstatus = 1;
$stmt->bind_param('sss', $userthatisfollowed, $theuserid, $followstatus);
$stmt->execute();
echo $response = "<span>Followed!</span>";
$stmt->close();
} else if(isset($_POST["submitUnfollow"])){
$userthatisfollowed = $_POST["thefollower"];
$theuserid = $_POST["theuserid"];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare('DELETE userfollow FROM userfollow WHERE thefollower = ? AND theuserid = ?');
$stmt->bind_param('ss', $userthatisfollowed, $theuserid);
$stmt->execute();
echo $response = "<span>Unfollowed!</span>";
$stmt->close();
} else {
echo "DID NOT WORK";
}
profile.php
if(isset($_SESSION["userid"]) && $_SESSION["userid"] != $userthatisfollowed) {
?>
<form action="<?php echo htmlspecialchars("includes/followuser.inc.php");?>" id="followform" method="post">
<?php
//if record is found in table, show Unfollow button, else Follow
if ($result->num_rows > 0){
$subscribe_status = "Unfollow";
} else {
$subscribe_status = "Follow";
}
//Here the button should change to Unfollow or Follow
echo "<button name='submit".$subscribe_status."' id ='followbutton' type='submit'>";
echo "<span>Follow</span>";
echo "</button>";
//this is just a notification bell for later
echo "<button name='submit".$subscribe_status."' id ='notificationbell' type='submit'>";
echo "<i class='fa fa-bell'></i>";
echo "</button>";
//the feedback message
echo "<div id='followmessage'></div>";
?>
<input type="hidden" name="theuserid" id="theuserid" value="<?php echo $_SESSION["userid"] ?>">
<input type="hidden" name="thefollower" id="thefollower" value="<?php echo $userthatisfollowed; ?>">
</form>
<?php
}
值得注意的是,我得到的响应DID NOT WORK
告诉我if(isset($_POST["submitUnfollow"]))
没有设置,但是,如果我尝试使用if(isset($_POST["theuserid"]) && (isset($_POST["thefollower"]))
,它实际上对插入查询有效,但对删除查询无效。
1条答案
按热度按时间wtzytmuj1#
data:
对象中缺少submitFollow
参数,而是PHP代码中没有使用的followbutton: true
,因此将其更改为:对于unfollow按钮,请使用
submitUnfollow
。