PHP follow / unfollow按钮正在工作,但需要刷新才能看到结果

时间:2017-04-20 16:43:50

标签: php html sql

我创建了一个关注/取消关注系统,用户可以关注其他用户,如果他跟踪他,他可以在以后决定取消关注他。根据点击的按钮插入和删除数据库内容的sql / mysql正在运行。但是,由于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 个答案:

答案 0 :(得分:0)

试试这个 - &gt; 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);