本文介绍了使用Ajax进行PHP flush循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉重复的问题,但是给定的答案对我不起作用。

Sorry for duplicate question, but the given answers do not work for me.

到目前为止,我点击按钮和ob_flush()之后使用Ajax执行PHP一个接一个地冲出echo()。然而,我看到我的回声即将到来。以下是我的代码:

So far I use Ajax to execute PHP after a button click and ob_flush() to flush out the echo() one after each other. However I see my echos coming all at once nevertheless. Below is my code:

PHP:


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();
?>

AJAX:

AJAX:

<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>

感谢您的帮助!
Peter

Thank you for your help!Peter

推荐答案

implicit-flush会帮助你。

implicit-flush will help you.

并且,你将'xmlhttp.readyState == 4'修改为'xmlhttp.readyState == 3'

And, you modify 'xmlhttp.readyState==4' to 'xmlhttp.readyState==3'

这篇关于使用Ajax进行PHP flush循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:43