0

I`m developing a php console application based sql database syncronizer. It's basically an update from one db to another. The console app is fine, but I'd like to make a webpage with live response of the 10 minutes job. Like:

Updating product#1 .... READY!

Updating product#2 .... 

My question is how can I do this with AJAX ? Is there some event that fires a respond from inside a php iteration not after it? Or I have to make an iteration of AJAX requests?

Thank you!

4

2 回答 2

1

It's no events called at PHP side, what you can use at client side to trigger JS functions. At least, it's can't be done easy. Sure you can use some like a Comet to get really interactive result. But think much easer do it as following: You php script will proceed only one product per call based on product_id from $_GET param. And this script, after proceeding requested product will return ID of next product to proceed. So, you just call by AJAX first time your proceed.php scrip, onComplete event this AJAX call shows your message "Updating product#1 .... READY!", getting ID of next product to proceed. And it's until all are done :)

于 2013-06-05T10:56:32.287 回答
0

there is no any event like this but you can get the progress percentage during the ajax call as follow

 $.ajax({
                        xhr: function()
                        {
                            var xhr = new window.XMLHttpRequest();
                            //Upload progress
                            xhr.upload.addEventListener("progress", function(evt){
                                if (evt.lengthComputable) {
                                    var percentComplete = (evt.loaded / evt.total)*100;
                                    //Do something with upload progress
                                    var per= Math.round( percentComplete );

//per gives you percentage of progress

                                }
                            }, false);
                            //Download progress
                            xhr.addEventListener("progress", function(evt){
                                if (evt.lengthComputable) {
                                    var percentComplete = (evt.loaded / evt.total)*100;
                                    //Do something with download progress
                                    var per= Math.round( percentComplete );
                                      //per gives you percentage complete of process

                                }
                            }, false);
                            return xhr;
                        },
                        url:'your url here',
                        data:{'data':data},
                        type:"POST",
                        success:function(n){
                           //ajax completion handler
                        }
                    });
于 2013-06-05T12:10:55.827 回答