PHP / JQuery - 仅当点击列表中的用户时才加载数据

PHP / JQuery - Load data only if clicked in an user on the list

嘿,好吧,我正在一个包含在线用户的页面中制作一个消息系统,现在我只想在我点击一个不在页面加载上的用户时加载旧消息,因此无法连接 slower.Any帮助? 谢谢

您可以通过 Ajax 请求来做到这一点。

我给你举个简单的例子。

您需要两个文件:

index.php

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>Main Page</title>
</head>
<body>
    <ul>
        <li class="users" id="1">First User</li>
        <li class="users" id="2">Second User</li>
    </ul>

    <div id="result"></div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        $( document ).ready(function() {
            $("li").click(function(event) {
                var user_id = event.target.id;
                $.ajax({
                    type: "POST",
                    url: "request_data.php",
                    data: "id=" + user_id,
                    dataType: "html",
                    success: function(msg)
                    {
                      $("#result").html(msg);
                    },
                    error: function()
                    {
                      alert("Error: ajax call failed.");
                    }
                });
            });
        });
    </script>
</body>
</html>

request_data.php

<?php
// Get user id submitted information
$user_id = $_POST["id"];
// here you could make your query to the db and stamp the result
echo "The id of the user is: ".$user_id;
?>

希望对您有所帮助。 :)