参考php页面上的位置


Refer to position on page php

我正在用html, php和javascript制作社交媒体。现在我实现了一个like函数,所以当你点击like按钮时它会转到一个php函数,它会添加一个like。但是当php函数指向post页面时,它指向页面的顶部。是否有一种可能的方法可以引用回喜欢的帖子?

谢谢!

正如Dagon所说,使用AJAX(最好是jQuery AJAX,使您更容易)将是最好的选择。下面是一小段代码:

like.php (POST):

<?php
include 'DatabaseConn.php'; //File that contains the database bridge, let's pretend $bridge is the bridge
if ( !isset($_SESSION['userid'] ) // We verify the user is logged in, by making sure we can access his user ID
    $error[] = 'Not logged in';
if ( !isset($_POST['post_id'] ) ) // We verify that a post ID was provided
    $error[] = 'Error, no post ID was provided';
if ( !isset($error) )
{
    $query = $bridge->query("SELECT * FROM posts WHERE post_id = '".$_POST['post_id']."' LIMIT 1"); //Assuming you're using MySQLi
    //I'm going to pretend that there's a column called LIKERS and that each liker is separated by two colons
    $data = $query->fetch_array(MYSQLI_ASSOC);
    $likers = explode("::", $data['likers']);
    $likers_str = $data['likers']; //We'll use this later
    if ( $query->num_rows < 1 ) // We make sure the post exists
    {
        echo json_encode(array("success" => false, "errors" => "The specified post does not exist"));
    }
    else
    {
        if ( in_array($_SESSION['userid'], $likers) ) //We find out if the user has liked the post already
        {
            echo json_encode(array("success" => false, "errors" => "You have already liked this post"));
        }
        else
        {
            $query = $bridge->query("UPDATE posts SET likes += 1 WHERE post_id = '".$_POST['post_id']."' LIMIT 1"); // Add the like
            $query2 = $bridge->query("UPDATE posts SET likers = '".$likers_str."::".$_SESSION['userid']."' WHERE post_id = '".$_POST['post_id']."' LIMIT 1"); // Add users to likers list
            if ( $query && $query2 ) //We verify both queries were successful
                echo json_encode(array("success" => true, "errors" => null));
            else
                echo json_encode(array("success" => false, "errors" => "There has been an error while liking this post"));
        }
    }
}
else
{
    echo json_encode(array("success" => false, "errors" => implode(", ", $error)));
}

like.js (AJAX):

$(document).ready(function() //Assuming you use jQuery
{
    $(".likeButton").on('click', function() //Click on like button
    {
        $.ajax({
        method: "POST",
        url: "like.php",
        data: { post_id : $(this).attr('id') }, //We grab the post id from the button
        dataType: "json"
        })
        .done(function( data ) {
            if ( data.success == "true" )
                alert('Post liked'); 
            else
                alert('Error, could not like post: ' + data.errors);
        });
        //After executing it I'd recommend disabling the button
    })
});

然后,如果你对如何创建按钮感到好奇,它应该这样创建:

<div class="likeButton" id="postid, something like 39128535">Like</div>

祝你项目顺利。

当然,当你点击"喜欢"按钮时,传递一个GET参数(like.php?liked=#id1),然后,在url的末尾使用id重定向返回:

$hash = $_GET['liked'];
header("Location: where/likes/are.php{$hash}");

#id1应该是一个元素的DOM id,你希望你的页面在哪里滚动。

注意,这只是一个粗略的例子。但是我强烈建议使用AJAX来执行这类过程。