如何用php echo显示长轮询结果 [英] how to show long poll result with php echo
本文介绍了如何用php echo显示长轮询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个长轮询脚本,如果我附加任何文本进行测试,该脚本运行良好。现在我想显示来自data update.php的user.php collect的长轮询结果。我在user.php页面上使用了我的轮询脚本。
My am working with a long poll script which working well if I append any text to test. Now I want to show long polling result of user.php collect from data update.php. I used my polling script at user.php page.
在这里,如果我使用test polling,如下所示,它运作良好
Here, If I used "test polling" as below, Its work well
$("#updatepost").append("test polling");
但我想在此附加我的update.php文件的echo
But I want append here my update.php file's echo
我的完整代码
在我的user.php
my full code In my user.php
<script>
function addmsg(type, msg){
$("#updatepost").append("test polling");
}
function waitForMsg(){
/* This requests the url "msgsrv.php"
When it complete (or errors)*/
$.ajax({
type: "GET",
url: "update.php",
async: true, /* If set to non-async, browser shows page as "Loading.."*/
cache: false,
timeout:50000, /* Timeout in ms */
success: function(data){ /* called when request to barge.php completes */
addmsg(".upbox1", data); /* Add response to a .msg div (with the "new" class)*/
setTimeout(
waitForMsg, /* Request next message */
1000 /* ..after 1 seconds */
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg, /* Try again after.. */
15000); /* milliseconds (15seconds) */
}
});
}
$(document).ready(function () {
waitForMsg(); /* Start the inital request */
});
</script>
</head>
<body>
<div id="updatepost">
</div>
在我的update.php中
In my update.php
$u = mysqli_query($dbh,"SELECT * FROM updateside WHERE `parent_id`='".$parent."' AND `created` > '".$timestamp."' ORDER BY created DESC") or die(mysqli_error($dbh));
while ($row = mysqli_fetch_array($u)) {
$parent_id = $row['parent_id'];
$to_id = $row['to_id'];
$sub = $row['sub'];
$detail = $row['detail'];
$time = $row['created'];
// I want to show this result at my polling
echo '<div class="upbox1" id="'.$parent_id.'"><div class="upbox2">'.$from_id.' '.$sub.' '.$to_pname.'</div>
<div class="upbox3">';
if ($detail=="") {echo '';}
else echo''.$detail.;
echo'</div></a><div class="upbox4">'.$time.'</div>
</div>';
}
推荐答案
如果您更改 addmsg
function to
If you change your addmsg
function to
function addmsg(type, msg){
$("#updatepost").append(msg);
}
它会在 #updatepost
元素。
这篇关于如何用php echo显示长轮询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文