douguio0185 2015-02-03 14:11
浏览 50

ajax实时更新,无需刷新php页面

I'm trying to post some data to post.php and I want the result to display in form.html, I used Ajax to get php output without needing to reload the page, what I couldn't figure out is how to flush some of the php output before the php script finishes.

I tried flush() and ob_flush() but it seems that they don't work when used with Ajax. My goal is to update form.html in real time without reloading the page.

Here is an example of what I'm trying to achieve. Source Code:

form.html

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#myform").validate({
                submitHandler: function(form) {
                    // do other stuff for a valid form
                    $.post('post.php', $("#myform").serialize(), function(data) {
                        $('#results').html(data);
                    });
                }
            });
        });
    </script>    
</head>
<body>
    <form name="myform" id="myform" action="" method="POST">  
    <!-- The Name form field -->
        Name:
        <input type="text" name="name" id="name" size="30" value=""/>  
        <br>
    <!-- The Email form field -->
        Email:
        <input type="text" name="email" id="email" size="30" value=""/> 
        <br>
    <!-- The Submit button -->
      <input  type="submit" value="Submit"/></div>
    </form>

    <!-- Result goes here -->
    <div id="results"><div>
</body>
</html>

post.php

<?php

for($i=0;$i<2;$i++) {
    echo "Your Name is ".$_POST['name']. "<br/>";
    echo "Your E-mail is".$_POST['email']."<br/>";
    echo "Waiting 2s <br/><br/>";
    //I want to print the above before waiting 2 seconds.
    sleep(2);// in real application this will be equivalent of some calculation that takes time.
}
?>
  • 写回答

2条回答 默认 最新

  • dongze8698 2015-02-03 14:28
    关注

    Your echos may already be sent to the client browser, but the client will be kept waiting for further output until your PHP script actually finishes. You need to disconnect the client early, eg: before sleep(2). Try this:

        ignore_user_abort(true);
        // Disconnect with the client
        if (!connection_aborted()) {
            $response = 'your response';
            header('Content-Length: ' . strlen($response));
            header('Connection: close');
            echo $response;
            flush();
        }
    

    Note that please do not do other echos before above code block. All your output should be in $response, since Content-Length must be calculated correctly.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向