在仍处理php服务器中的数据时向浏览器发送反馈
hello in my code i send a bunch of data to the server with the json from the javascript,in the server i will store this data to the mysql database,because i'am going to store the data one rows at a time i want to make the client aware of the progress made in the server side,the code is
for($j=0 ; $j < $number_rows; ++$j){
$result=mysql_query("INSERT INTO table_name(column_name)VALUES('value')");
if(!$result)
//here i want to send the browser that the $j rows is saved and
//that for every iteration
}
so is that possible or the server wouldn't be able to send the data until the script is all terminated.
You can call flush() in the mean time, although it doesn't always work.
The detailed description of caveats can be found on the documentation page I linked before. In short, flush
will try to send (flush) all current output to the browser, but other process, like PHP output buffering, Apache compression layers, and even buffering by the browser itself may cause the output to not appear in the browser immediately.
it seems you are not aware of $.ajax() method of jquery. It allows you to send the data to the server and keep the control in the browser.
You can use it like this:
$.ajax({
type: 'POST',
url: 'insert-data.php',
data: data,
beforeSend: function() {
// setting a timeout
$('.sending').addClass('Sending data...');
},
success: function(data) {
if (data==1) {
$('.sending').html('Saved data successfully.');
} else {
$('.sending').html('Oops..! Something went wrong!!!');
}
}
</div>