嘿,我正在一个包含在线用户的页面中创建一个消息系统,现在我只想在点击未加载页面的用户时加载旧消息,这样连接就不会变慢。有什么帮助吗?感谢
您可以通过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;
?>
希望提供帮助。:)