每次迭代后更新ajax php foreach循环
我试图做的是使用jQuery/Ajax向PHP脚本发出请求,并在每次每次迭代后更新ajax php foreach循环,php,jquery,ajax,foreach,Php,Jquery,Ajax,Foreach,我试图做的是使用jQuery/Ajax向PHP脚本发出请求,并在每次foreach循环完成时返回状态更新。我想使用输出制作一个进度条 我遇到的问题是,jQuery只会在整个脚本完成后更新页面。有没有一种方法可以强制jQuery或PHP在每个循环上输出,而不仅仅是将其全部转储到success函数上 jQuery(document).ready(function($) { $(document).on('click','#fetch-s
foreach
循环完成时返回状态更新。我想使用输出制作一个进度条
我遇到的问题是,jQuery只会在整个脚本完成后更新页面。有没有一种方法可以强制jQuery或PHP在每个循环上输出,而不仅仅是将其全部转储到success
函数上
jQuery(document).ready(function($) {
$(document).on('click','#fetch-snowfall-data a',function () {
$(this).text('Fetching Snowfall Data...');
$.ajax({
type: "GET",
async: true,
url: "url.php",
data:{},
dataType: "html",
success: function(response){
$("#response_container").append(response);
$('#fetch-snowfall-data a').text('Fetch Snowfall Data');
}
});
return false;
});
});
PHP
正如adeneo所建议的,我认为你不能返回部分内容。我以前试过对此进行研究,但没有结果。不过,如果有人能纠正我,我将非常感激 对我来说,最终的结果是做递归ajax javascript
var data = '';
$(document).on('click','#fetch-snowfall-data a',function () {
ajaxCall(0);
});
function ajaxCall(num) {
$.ajax({
type: "POST",
async: true,
url: "url.php",
data: num,
dataType: "json",
xhr:function(){
//progress bar information here.
},
success: function(response) {
$("#response_container").append(response['data']);
if (response['check'] === 'next') {
var nextNum = num +1;
data += response['data'];
ajaxCall(nextNum); //call itself for the next iteration
}else if (response['check'] === 'done'){
data = response['data'];
//do stuff with the data here.
}else if (response['check'] === 'error') {
return response['data'];
}
},
error:function(xhr, status,error){
//error information here
}
});
}
对于PHP:
只需确保在json中输出包含两个信息的内容Json。检查
:查看是否要进入下一个迭代,完成并输出数据,或报告错误Json.data
以输出所需的任何数据。您需要做的是一次输出每个报告,而不使用foreach
循环
--编辑--
我找到了一些关于php流媒体的主题
谢谢大家的帮助。我最终设法让这一切正常工作,并将分享我如何做到这一点,为其他任何人的未来利益 需要3个文件-从中加载的页面、处理脚本和侦听脚本 您需要知道要处理多少行才能使所有内容正常工作。我从php变量
$results['total_rows']加载我的
jQuery(document).ready(function($) {
setTimeout(getProgress,1000);
$(document).on('click','#fetch-snowfall-data a',function () {
$(this).text('Fetching Snowfall Data...');
$.ajax({
url: 'process.php',
success: function(data) {
$("#response_container2").append(data);
}
});
setTimeout(getProgress,3000);
return false;
});
function getProgress(){
$.ajax({
url: 'listen.php',
success: function(data) {
if(data<=<?php echo $results['total_rows']; ?> && data>=1){
console.log(data);
$('#response_container').html('<div id="progress"><div class="progress-bar" role="progressbar" style="width:'+ (data / <?php echo $results["total_rows"] ?>)*100 +'%">'+ data + '/' +<?php echo $results["total_rows"] ?> +'</div></div>');
setTimeout(getProgress,1000);
console.log('Repeat');
} else {
$('#fetch-snowfall-data a').text('Fetch Snowfall Data');
console.log('End');
}
}
});
}
});
Loading.php
jQuery(document).ready(function($) {
setTimeout(getProgress,1000);
$(document).on('click','#fetch-snowfall-data a',function () {
$(this).text('Fetching Snowfall Data...');
$.ajax({
url: 'process.php',
success: function(data) {
$("#response_container2").append(data);
}
});
setTimeout(getProgress,3000);
return false;
});
function getProgress(){
$.ajax({
url: 'listen.php',
success: function(data) {
if(data<=<?php echo $results['total_rows']; ?> && data>=1){
console.log(data);
$('#response_container').html('<div id="progress"><div class="progress-bar" role="progressbar" style="width:'+ (data / <?php echo $results["total_rows"] ?>)*100 +'%">'+ data + '/' +<?php echo $results["total_rows"] ?> +'</div></div>');
setTimeout(getProgress,1000);
console.log('Repeat');
} else {
$('#fetch-snowfall-data a').text('Fetch Snowfall Data');
console.log('End');
}
}
});
}
});
Listen.php
session_start();
echo (!empty($_SESSION['progress']) ? $_SESSION['progress'] : '');
if (!empty($_SESSION['progress']) && $_SESSION['progress'] >= $_SESSION['total']) {
unset($_SESSION['progress']);
}
进度条的一些CSS
#progress {
height: 20px;
margin-bottom: 20px;
/* overflow: hidden; */
background-color: #f5f5f5;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
}
.progress-bar {
float: left;
width: 0;
height: 100%;
font-size: 12px;
line-height: 20px;
color: #fff;
text-align: center;
background-color: #337ab7;
-webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);
box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);
-webkit-transition: width .6s ease;
-o-transition: width .6s ease;
transition: width .6s ease;
}
不太可能,您可能需要SSE、套接字或为此进行轮询,您通常不能返回部分内容(但可以使用刷新)。一个简单的解决方案是添加beforeSend:
属性,并放置一个函数,该函数将使用微调器gif或其他内容填充容器,这样至少人们会看到它正在做。您需要一个更复杂的jquery:来获得实际的进度。@Rasclatt我之前尝试过,但无法从中获得任何输出。它没有详细说明任何服务器端代码,所以我不确定如何构造output@adeneo我试着用法拉盛,但没有用。我一直在研究SSE,但还没有找到相关的示例或教程。如果您知道任何please shareSo,那么示例中列出的console.log(percentComplete)
没有在您的控制台中生成任何内容?这在coldfusion中是可能的,但我不是php开发人员,因此不知道是否可以使用php完成。查看数据流和/或刷新缓冲区。@KevinB谢谢,我一定会查看的。似乎运行nodejs会比php好?两者都可以做到,所以这取决于个人喜好。我更喜欢node.js
#progress {
height: 20px;
margin-bottom: 20px;
/* overflow: hidden; */
background-color: #f5f5f5;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
}
.progress-bar {
float: left;
width: 0;
height: 100%;
font-size: 12px;
line-height: 20px;
color: #fff;
text-align: center;
background-color: #337ab7;
-webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);
box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);
-webkit-transition: width .6s ease;
-o-transition: width .6s ease;
transition: width .6s ease;
}