duanque3125 2017-04-16 23:41
浏览 23

如何从函数调用php刷新我当前的页面?

After the user hits the follow/unfollow button the same button appears (because the page is not refresh, and if the user clicks on the button again an error will pop since the user is trying to access a duplicate field) However i want the page to refresh after any of the buttons are clicked this is the code i have for both the php and the functions used:

            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()){
                header("Refresh:0");
                echo "<h1 style='clear:left;font-size:15px; color:blue;'>UnFollowed</h1>";  
            }
        }
        }


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()){ 
        header("Refresh:1");
            echo "<h1 style='clear:left;font-size:15px; color:blue;'>Followed</h1>";    
        }

         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>"; 
       }
   }
}

I am using it in the following format in php:

    <?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" style="clear:left;" action="">

     <button id="follow_btn" type="submit">Follow</button>

     </form>

   <?php 
    }

 //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" style="clear:left;" action="<?php                    follow($session_user_id, $uploaderid) ?>">

   <button id="follow_btn" type="submit" name="follow">Follow</button>

   </form>
   <?php 

  //else show the unfollow button
   }else{
    ?>
   <form id="follow" method="POST" style="clear:left;" action="<?php     unfollow($session_user_id, $uploaderid) ?>">
    <button id="follow_btn" type="submit" name="unfollow">UnFollow</button>
    </form>

    <?php
    }
    ?>

I am first checking if the user is not logged in, in that case just show a follow button that doesnt do a thing. Else if he is logged in but is not following the uploader then show the follow button. Else if the user is following show the unfollow button.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看