dongyi2159 2017-07-05 11:48
浏览 91
已采纳

执行后的PHP函数刷新页面(返回div)

I have got 2 pages:

Fun.php
Class.fun.php

In class.fun.php I got functions, for example, select posts/upvote post/downvote post.

In Fun.php I'm displaying all posts (Like facebook posts) : Image

When I upvote/downvote post, it calls function in class.fun.php and refreshes page, but goes back to top. : Image

But I want page to stay or go back to same post, where I clicked upvote , so I could scroll forward.

My Upvote function:

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);

        }

        $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条回答 默认 最新

  • duanhe4267 2017-07-05 12:26
    关注

    Take a look at HTML anchors for a very simple solution, assuming you are using a hyperlink for voting. You would need the container of each post to have it's own unique ID attribute, which you then refer to in your upvote/downvote href attribute, creating an anchor which the browser will jump to. See https://www.w3schools.com/html/html_links.asp For example -

    <div id="post_1">
        Post content is here
        <a href="yoururl.com/posts#post_1">UPVOTE</a>
        <a href="yoururl.com/posts#post_1">DOWNVOTE</a>
    </div>
    <div id="post_2">
        Post content is here
        <a href="yoururl.com/posts#post_2">UPVOTE</a>
        <a href="yoururl.com/posts#post_2">DOWNVOTE</a>
    </div>
    

    Alternatively look into using javascript ajax to post the data without reloading the page and therefore no need to scroll at all. See https://www.w3schools.com/xml/ajax_intro.asp for an introduction, or check out a JS library like jQuery.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码