2017-04-20 22 views
0

我創建了一個關注/取消關注系統,用戶可以關注其他用戶,如果他正在關注他,他可以稍後決定取消關注他。 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 
     } 

     ?> 
+1

可以使用AJAX調用顯示追隨者計數。 – Arjun

+0

你想刷新頁面提交嗎?將onClick =「window.location.reload()」添加到按鈕標記 – brad

+0

@brad我試過它不起作用,我認爲php首先運行忽略重新加載請求。 –

回答

0

試試這個 - > forceget將使它從服務器

把下面的表單這個地方重裝我覺得語法和括號內是正確的,你不要有純HTML可能很難檢查的jsfiddle

<script> 
$(document).ready(function() { 

$('#follow_btn').click(function() { 
    location.reload(forceGet); 
    }); 

$('#unfollow_btn').click(function() { 
    location.reload(forceGet); 
    }); 
}); 
</script> 

你可能需要包裝在設置超時功能點擊事件,以避免需要刷新頁面太快

setTimeout(function() { 
    $('#follow_btn').click(function() { 
     location.reload(forceGet); 
     }); 
}, 300); 
+0

嗨,布拉德,很好的例子,不幸的是它不工作。 –

+0

可能不是這種情況,但是它有時候會包含jquery腳本鏈接嗎?還有一個手動刷新修復它? – brad

+0

手動刷新確實修復它,但這種方法實際上並沒有刷新頁面(在php工作運行後) –