在函数执行后我不希望我的php页面刷新

时间:2017-08-06 10:32:00

标签: javascript php jquery ajax

我有收集和显示所有帖子的功能,每个帖子都有upvote / downvote按钮。

点击按钮,我调用了名为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();
    }               
}

2 个答案:

答案 0 :(得分:0)

基本上你必须创建一个php文件,它将把呼叫路由(让它称之为控制器)到预期的功能。然后创建一个将触发该控制器的ajax函数。看一下 Ajax Intro 或者查看Jquery Implementation

答案 1 :(得分: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
                }
      });
  });


});