2013-12-14 33 views
2

我有问题显示当前插入到MySQL与查询。 现在我在此的index.php代码:PHP + Mysql插入+ jQuery进度条

$('#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我有PHP代码与插入数据的MySQL与()。 $i是当前插入行,并且$pocet是总行数。

问题是显示当前插入行与ajax。 上面的代码显示在div #progress“100”当ajax完成加载。

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

回答

1

我认为php - > flush()是你在找什么。

这个例子完美的作品在我的PHP的Apache在Debian

看到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> 

也有看看罗杰斯在回答how-to-flush-output-after-each-echo-call 他在apache设置中发现问题

+0

我也试过,但现在为我工作。我不知道为什么。 –

+0

请参阅在线演示:http://codebook.cz/ ..进度条是带ID的#div进度..不显示任何东西 –

+0

只是试过!作品,但播种1秒钟的神秘文本步骤变化:

Begin ...
0
2„P˙˙2‚P˙˙2†P˙˙2P˙˙2…P˙˙2P˙˙2‡P˙˙˛€P˙˙˛„P˙˙rÍKA \t ˙˙UˇAVb
Erik255