javascript - 引用php页面上的位置

标签 javascript php html reference

<分区>

我正在使用 html、php 和 javascript 制作社交媒体。现在我正在实现一个类似的功能,因此当您单击“类似”按钮时,它会转到一个 php 函数并添加一个类似的功能。但是当 php 函数返回到帖子页面时,它指的是页面顶部。是否有可能返回到喜欢的帖子?

谢谢!

最佳答案

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

祝你的项目好运。

关于javascript - 引用php页面上的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30585848/

相关文章:

javascript - React - 使用和加载视频文件

php - 如何添加对小数点前 3 位正 float 的验证?

php - centos如何在浏览器中打开HTML文件?

foreach php 中的 Javascript - Magento

javascript - 使用 JQuery 动态加载 ASP.NET MVC 局部 View

PHP - 空 $_POST 和 $_FILES - 上传较大文件时

javascript - 在 Svelte 中向下传递 Prop

javascript - 让基于 HTML 5 的 VR 应用在 Daydream 中显示

javascript - 正则表达式获取每行的第一个元素

javascript - 跨域请求 - javascript