如何用php echo显示长轮询结果

时间:2014-12-20 17:23:59

标签: javascript jquery

我正在使用一个长轮询脚本,如果我附加任何文本进行测试,该脚本运行良好。现在我想显示来自data update.php的user.php collect的长轮询结果。我在user.php页面上使用了我的轮询脚本。

在这里,如果我使用如下所示的“测试轮询”,它的效果很好

    $("#updatepost").append("test polling");

但是我想在这里附上我的update.php文件的echo

我的完整代码 在我的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中

    $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>';
}

1 个答案:

答案 0 :(得分:0)

如果您将addmsg功能更改为

function addmsg(type, msg){
    $("#updatepost").append(msg);
}

它会将你的ajax调用的结果附加到#updatepost元素中。