2017-08-06 68 views
0

我收到並顯示所有帖子的功能,並且每個帖子都有upvote/downvote按鈕。我不希望我的php頁面在執行功能後刷新

點擊按鈕,我調用upvotePost和downvotePost函數。

它一切正常,但它刷新頁面,我想了解如何使它不刷新頁面。

我知道它是由ajax/jquery完成的,但不知道如何製作它。

我的按鈕,例如:

<a href="fun.php?upvote-btn=true?action=select&image_id=<?php echo $post['id'];?>"> 

函數調用:

if(isset($_GET['upvote-btn'])){ 
    $fun->upvotePost();  
} 

我的功能:

public function upvotePost(){ 
    try 
    { 

     if(isset($_SESSION['user_session'])){ 
      $user_id = $_SESSION['user_session']; 

      $stmt = $this->runQuery("SELECT * FROM users WHERE id=:id"); 
      $stmt->execute(array(":id"=>$user_id)); 

      $myRow=$stmt->fetch(PDO::FETCH_ASSOC); 

     }else{ 
      $_SESSION["error"]='Sorry, You have to login in you account!'; 
     } 

     $id = $_GET['image_id'];      
     $user_id = $myRow['id']; 

     $stmt2 = $this->conn->prepare("SELECT count(*) FROM fun_post_upvotes WHERE image_id=('$id') AND user_id=('$user_id')"); 
     $stmt2->execute(); 
     $result2 = $stmt2->fetchColumn(); 

     if($result2 == 0){ 

      $stmt3 = $this->conn->prepare("INSERT INTO fun_post_upvotes (image_id,user_id) VALUES(:image_id,:user_id)"); 

      $stmt3->bindparam(":image_id", $id); 
      $stmt3->bindparam(":user_id", $user_id); 

      $stmt3->execute(); 

      $stmt4 = $this->conn->prepare("SELECT * FROM fun_posts WHERE id=('$id')"); 
      $stmt4->execute(); 
      $result4 = $stmt4->fetchAll(); 

      foreach($result4 as $post){ 
       $newUpvotes = $post['upvotes']+1; 

       $stmt5 = $this->conn->prepare("UPDATE fun_posts SET upvotes=$newUpvotes WHERE id=('$id')");                       
       $stmt5->execute(); 

       $_SESSION["result"]='You have succesfully liked this post!'; 

      } 
      }else{ 

      $_SESSION["error"]='You have already liked this post!'; 

      } 

      $stmt6 = $this->conn->prepare("SELECT count(*) FROM fun_post_downvotes WHERE image_id=('$id') AND user_id=('$user_id')"); 
      $stmt6->execute(); 
      $result6 = $stmt6->fetchColumn(); 

      if($result6 > 0){ 
       $stmt7 = $this->conn->prepare("DELETE FROM fun_post_downvotes WHERE image_id=('$id') AND user_id=('$user_id')"); 
       $stmt7->execute(); 

       $stmt8 = $this->conn->prepare("SELECT * FROM fun_posts WHERE id=('$id')"); 
       $stmt8->execute(); 
       $result8 = $stmt8->fetchAll(); 

       foreach($result8 as $post){ 

        $newDownvotes = $post['downvotes'] - 1; 

        $stmt9 = $this->conn->prepare("UPDATE fun_posts SET downvotes=$newDownvotes WHERE id=('$id')");                       
        $stmt9->execute(); 

       } 
      } 
    } 
    catch(PDOException $e) 
    { 
     echo $e->getMessage(); 
    }    
} 
+1

你有什麼試圖讓它在AJAX中工作?有很多關於SO的解釋和通過谷歌來引導你完成AJAX請求。 –

+0

我從來沒有學過AJAX,但最近幾周試圖弄清楚它是如何工作的,但無法理解。 – Crelix

回答

0

Ajax是你的問題的完美答案。請看看這個。您可能需要根據您的要求更改此片段。

在這之前你應該導入jquery。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> 

您給予好評按鈕應該是這樣的,

<button id="upvote"> upvote </button> 

在JavaScript部分添加以下代碼片段。

$(function(){ 

    $("#upvote").click(function(){ 
    $.ajax(
     { url: "fun.php?upvote-btn=true?action=select&image_id=<?php echo $post['id'];?>", 
     type: "get", 
     success: function(result){ 
        // todo something you need to perform after ajax call 
       } 
     }); 
    }); 


}); 
0

基本上,你必須創建(一個PHP文件,該文件將呼叫路由讓我們稱之爲控制器)到預期的功能。然後創建一個ajax函數,該函數將會觸發該控制器。看看 Ajax Intro 或看看Jquery Implementation

相關問題