当用户登录时,我想运行bellow php页面。我使用bellow php代码显示他们的名字的在线访问,我也链接他们的个人资料页面与它。
当online.php重新加载时,php页面正在运行。
我知道,这是php的正常方式,但我想使用ajax来运行bellow php脚本。我知道如何使用ajax发送表单数据,但如果页面中没有表单,我不知道如何发送数据。
这怎麽可能?我试过了但还没起作用。Note:我也在使用JQuery。
AJAX代码
//update the online users list
$.ajax({
type: "POST",
url: "online.php",
data: data,
success: function (response) {
$("#online").html(response);
}
}); //end itPHP代码
<?php
session_start();
include('controller/class/RO_dbconfig.php');
$sth = $dbconnect->prepare("SELECT first_name,keyId FROM register WHERE status = :online");
$params = array("online" => 1);
$sth->execute($params);
$status = $sth->fetchAll();
echo "<div id='online'>";
foreach($status as $onlineArray) {
echo "<ul class='online'>";
echo "<li><a href='timeline.php?profileId=".$onlineArray['keyId']."'>".$onlineArray['first_name']."</a></li>";
echo "</ul>";
}
echo "</div>";
?>发布于 2013-04-25 14:47:31
如果我正确地理解了您,这将帮助您检查是否有一个新用户在线,然后使用或不使用POSTed表单数据获取您的联机列表。
<script type="text/javascript">
// example variable; you can set this to the ID of the last stored online session in your DB
// the variable will be referenced later to see if any new sessions have been added
var lastConnectionId = 446;
// this function will fetch your online list; if a form has been submitted you can pass the serialized POST data
// else you can pass a null variable
function getOnlineList(data) {
var requestType = (data == null) ? "GET" : "POST";
$.ajax({
type: requestType,
url: "online.php",
data: data,
success: function (responce) {
$("#online").html(responce);
}
});
}
// this function will check to see if any new sessions have been added (i.e. new online users) by comparing to last
// session row ID in the database to the variable lastConnectionId; there must be a table in the database that stores
// sessions and gives them incremental IDs; check-sessions.php should output the latest session ID
function checkSessions() {
$.ajax({
type: "GET",
url: "check-sessions.php",
success: function (responce) {
if (responce > lastConnectionId) {
lastConnectionId = responce;
getOnlineList(null);
}
}
});
}
// runs checkSessions ever 3 seconds to check for a new user
setInterval("checkSessions", 3000);
</script>当然,在必要时添加所有其他代码和监听器等。
发布于 2013-04-25 14:42:14
你应该使用load()
$("#online").load("online.php");或者你可以使用get (在你的情况下比post更好):
$.ajax({
type: "GET",
url: "online.php",
data: {},
success: function (responce) {
$("#online").html(responce);
}
});发布于 2013-04-25 14:45:24
当用户联机时,不能使其更新。您必须在计时器的帮助下轮询online.php。执行定期编写的代码,如下所述:Server polling with JavaScript
https://stackoverflow.com/questions/16217587
复制相似问题