我在使用查询将当前插入的行显示到 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/