请参阅页面上的位置php

时间:2015-06-02 01:21:24

标签: javascript php html reference

我正在使用html,php和javascript制作社交媒体。现在我正在实现一个类似的功能,所以当你点击相似的按钮时,它会转到一个php函数,它会添加一个类似的功能。但是当php函数返回到帖子页面时,它指的是页面顶部。有没有办法可以参考喜欢的帖子?

谢谢!

2 个答案:

答案 0 :(得分:1)

正如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>

祝你的项目好运。

答案 1 :(得分:0)

当然,当您点击“赞”按钮时,请传递GET参数(like.php?liked=#id1),然后只需重新定向,并在网址末尾添加ID:

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

#id1应该是您希望滚动页面的元素的DOM ID。

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

相关问题