簡體   English   中英

使用 Ajax 的 PHP 刷新循環

[英]PHP flush loop with Ajax

抱歉重復的問題,但給定的答案對我不起作用。

到目前為止,我使用 Ajax 在單擊按鈕后執行 PHP,並使用 ob_flush() 一個接一個地清除 echo()。 然而,我看到我的回聲一下子傳來。 下面是我的代碼:

PHP:

ob_start();
echo "Server received this information from user: ". $debugMode. "<br>";
echo "Server answers this: <br>";
ob_flush();
for ($i = 1; $i <= 10; $i++) 
{
  echo( "Hello World ". $i. "<br>" );
  ob_flush();
  usleep(200000); //wait 0.2 seconds
}
ob_end_flush();
?>

阿賈克斯:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript"> 

    function runAjax(debugMode)
    {
        if (debugMode=="")
        {
            document.getElementById("outputPhp").innerHTML="nothing was send to server";
            return;
        }
        if (window.XMLHttpRequest)
        {
            // AJAX use with IE7+, Chrome, Firefox, Safari, Opera
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            // AJAX use with IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("outputPhp").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","TestButtonClick.php?q="+debugMode,true);
        xmlhttp.send();
    }
</script>
<title>Debug</title>
</head>

<body>
<br>
<input type="button" size="10" value="test" 
onclick="runAjax(this.value)"/>
<span id="test">Click this button to run all tests</span>
<br>
<br>
<p id="outputPhp"></p>
<br>
</body>

</html>

謝謝您的幫助! 彼得

https://www.php.net/manual/en/function.ob-implicit-flush.php

隱式刷新會幫助你。

並且,您將 'xmlhttp.readyState==4' 修改為 'xmlhttp.readyState==3'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM