2017-08-06 1 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で動作させるために何を試しましたか?あなたとAJAXのリクエストを作成するあなたを歩くgoogleを介して説明の負荷があります。 –

+0

私はAJAXを学んだことは一度もありませんが、ここ数週間、すべての仕組みを理解しようとしていますが、理解できません。 – Crelix

答えて

0

Ajaxはあなたの質問のために完璧な答えで見てください。これをチェックしてください。必要に応じてこのスニペットを変更する必要があります。

これを行う前に、jqueryをインポートする必要があります。

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

あなたupvoteボタンは、このようにする必要があり、

<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

関連する問題