douwei8672 2013-12-14 11:53
浏览 40

PHP + Mysql插入+ jQuery进度条

I have problem with showing current inserted rows into mysql with query. Now I have this code in index.php:

  $('#nacitatATC').click(function() {
    $.ajax({
            xhr: function() {

                    var xhr = new window.XMLHttpRequest();
                    xhr.addEventListener("progress", function(e){
                        var p = (e.loaded / e.total)*100;
                        var prave = $("#progress").html();
                              $("#progress").html(prave+"<br>"+p);
                    });
                return xhr;

            }
            , type: 'post'
            , cache: false
            , url: "/sql.php"});

      });
  });

and in sql.php I have the php code with inserting data into mysql with for(). $i is the current inserting line and $pocet is total lines.

The problem is with show current inserting line with ajax. The code above shows in div #progress "100" when ajax finish loading.

I need show 1% 2% 3% 4%... 99% 100% Completed. Thanks for help!

  • 写回答

1条回答 默认 最新

  • duanbiaoshu2021 2013-12-14 12:15
    关注

    I think php -> flush() is what you are looking for.

    This Example works perfect on my php-apache on debian

    see ajax-request-progress-percentage-log

    PHP side:

    header( 'Content-type: text/html; charset=utf-8' );
    echo 'Begin ...<br />';
    for( $i = 0 ; $i < 10 ; $i++ )
    {
        echo $i . '<br />';
        flush();
        ob_flush();
        sleep(1);
    }
    echo 'End ...<br />';
    

    site:& javascript:

    <!doctype html>
    <html>
    <head>
        <title>Test Flush</title>
        <meta charset="utf-8">
        <script src="./js/jquery-2.0.3.min.js" type="text/javascript"></script>
    <body>
    <h1>Hello</h1>
    <div id="progress">0%</div>
    
    <p>
    </p>
    <script type="text/javascript">
        $('h1').click(function() {
        $.ajax({
                xhr: function() {
    
                        var xhr = new window.XMLHttpRequest();
                        xhr.addEventListener("progress", function(e){
                            console.log(e.currentTarget.response);
                            $("#progress").html(e.currentTarget.response);
                        });
                    return xhr;
    
                }
                , type: 'post'
                , cache: false
                , url: "testFlush.php"
            });
    
        });
    
    </script>
    </body>
    

    Also have a look at Rogers Answer in how-to-flush-output-after-each-echo-call He found issues in the apache setup

    评论

报告相同问题?

悬赏问题

  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并