weixin_33681778 2017-02-27 08:13 采纳率: 0%
浏览 28

使用Ajax的PHP刷新循环

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

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:

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:

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

Thank you for your help! Peter

  • 写回答

1条回答 默认 最新

  • weixin_33696822 2017-02-27 08:31
    关注

    http://php.net/manual/kr/function.ob-implicit-flush.php

    implicit-flush will help you.

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

    评论

报告相同问题?