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'

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler