PHP + Mysql insert into + jQuery 进度条

标签 php jquery mysql ajax progress-bar

我在使用查询将当前插入的行显示到 mysql 时遇到问题。 现在我在 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"});

      });
  });

sql.php 中,我有使用 for() 将数据插入 mysql 的 php 代码。 $i为当前插入行,$pocet为总行数。

问题在于使用 ajax 显示当前插入行。 当ajax加载完成时,上面的代码显示在div #progress "100"。

我需要显示 1% 2% 3% 4%... 99% 100% 已完成。 感谢您的帮助!

最佳答案

我认为 php -> flush() 是您正在寻找的。

这个例子在我的 debian 上的 php-apache 上完美运行

参见 ajax-request-progress-percentage-log

PHP 方面:

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 />';

网站:&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>

另请参阅 Rogers Answer how-to-flush-output-after-each-echo-call 他在 apache 设置中发现了问题

关于PHP + Mysql insert into + jQuery 进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20582931/

相关文章:

php - 静态网页 vs MySql 生成

mysql - 将 Doctrine 2 与现有的 MySQL 数据库一起使用

php - 如何使用outlookrestapi访问outlook地址簿

php - 如何在 codeigniter 中播种数据

javascript - 我可以将 json 输入转换为 jquery 中的对象列表吗?

javascript - jqPlot - 基于系列中每个数据点的值范围的不同颜色条

php - 从 JS 或 CSS 文件中删除 Cookie

php - 显示数据库中用户表中的所有数据

php - 获取jquery触发事件中传递的变量的值

mysql - 如何从两个具有不同值的单独数据表中查询两个