我創建了一個關注/取消關注系統,用戶可以關注其他用戶,如果他正在關注他,他可以稍後決定取消關注他。 sql/mysql根據點擊按鈕插入和刪除數據庫內容正在工作。但是,由於PHP動態數據,用戶需要刷新才能查看相應的輸出(無論是跟隨/跟隨)。我該如何解決這個問題?PHP關注/取消關注按鈕正在工作,但需要刷新才能看到結果
這既是用我的功能:
功能的取消關注
//unfollow selected uploader if already following
function unfollow($userid, $uploader_id){
if(isset($_POST['unfollow'])){
$con = mysqli_connect('localhost', 'root', '', 'db');
$userid = sanitize($userid);
$uploader_id = sanitize($uploader_id);
//prepare statement
$stmt = $con ->prepare("DELETE FROM follow
WHERE u_id = ? AND uploader_id = ?");
$stmt -> bind_param("ss",$userid, $uploader_id);
if($result = $stmt->execute()){
}else{
echo "failed to unfollow";
}
}
}
這是功能取決於用戶是否是以下或跟隨
//use this function to follow an uploader
function follow($userid, $uploader_id){
if(isset($_POST['follow']))
{
if(logged_in()){
if($userid != $uploader_id){
$con = mysqli_connect('localhost', 'root', '', 'db');
$userid = sanitize($userid);
$uploader_id = sanitize($uploader_id);
//prepare statement
$stmt = $con ->prepare("INSERT INTO follow (u_id, uploader_id) VALUES (?, ?)");
$stmt -> bind_param("ss",$userid, $uploader_id);
if($result = $stmt->execute()){
}
else{
echo "Failed to follow";
}
}// users cant follow themselves
}//if not logged in do this
else{
echo "<h1 style='clear:left;font-size:15px; color:red;'>You have to be logged in to follow</h1>";
}
}
}
HTML/PHP的按鈕不是:
//if the user is not logged in just display a button that send him/her to the login page if clicked
if(!logged_in()){
?>
<form id="follow" action="login.php">
<button style="float:left;" class="follow_btn" type="submit">Follow</button>
</form>
<?php
}else if($uploaderid == $session_user_id){
}
//if the user is not following the uploader show the follow button
else if(!is_following($session_user_id, $uploaderid)){
?>
<form id="follow" method="POST" action="<?php follow($session_user_id, $uploaderid) ?>">
<button style="float:left;" id="follow_btn" class="follow_btn" type="submit" name="follow">Follow</button>
</form>
<?php
//else show the unfollow button
}else{
?>
<form id="follow" method="POST" action="<?php unfollow($session_user_id, $uploaderid) ?>">
<button style="float:left;" id="unfollow_btn" class="follow_btn" type="submit" name="unfollow">Following</button>
</form>
<?php
}
?>
可以使用AJAX調用顯示追隨者計數。 – Arjun
你想刷新頁面提交嗎?將onClick =「window.location.reload()」添加到按鈕標記 – brad
@brad我試過它不起作用,我認爲php首先運行忽略重新加載請求。 –