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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 Android URL如何转成视频/音频,可行吗?
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 关于#sql#的问题:没有用命令关闭cdc,手动把系统表开启的cdc右键删除了
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题