0

First of all I've seen and tested the solutions below and none of them worked (they do not retrieve any output).

  1. https://gist.github.com/db/966388
  2. What is the cleanest way to get the progress of JQuery ajax request?
  3. PHP ajax remote call to check progress

So, I'm trying to adapt the code to my needs.. my ajax is the following:

$.ajax({
    type: "POST",
    url: "/getAll",
    data: {name: name, age: age, from_where: from_where},
    async: true,
    success : function(data) {
        console.log(data);
    },
});

The problem with success function is that it is only executed after the whole PHP code is processed (at least for what I have tried).

$loaded = 0;
$total = 18; // just for testing

foreach($cust->find("NY") as $div){
    $cust->add($name, $div->description("NY"), $age);
    $loaded++;
    echo round(floatval(($loaded / $total) * 100), 2) . "%";
    // It can't be return instead of echo because it STOPs at the first index
}

The only problem with this code is that success function is only called after performed the whole PHP code. So, I'm seeking for ways to output the percentage during the loop of PHP.

Any ideas?

Community
  • 1
  • 1
Linesofcode
  • 5,327
  • 13
  • 62
  • 116

1 Answers1

0

my code I get somewhere on net is very similar the one you post in second link.

var jqXHR = $.ajax(
{
    xhr: function() 
    {
        var xhrobj = $.ajaxSettings.xhr();
        if (xhrobj.upload) 
        {
            xhrobj.upload.addEventListener('progress', function(event) 
            {
                if (status != null)
                {
                    var percent = 0;
                    var position = event.loaded || event.position;
                    var total = event.total;
                    if (event.lengthComputable) 
                    {
                        percent = Math.ceil(position / total * 100);
                    }
                    status.SetProgress(percent);
                }
            }, false);
        }
        return xhrobj;
    },
    url: uploadURL,
    type: "POST",
    success: function(data)
    {
        var diff = new Date() - time;
    },              
    error: function(jqXHR, textStatus, errorThrown )
    {
        alert(textStatus);
    },
    complete: function(jqXHR, textStatus)
    {
    }
}); 

This works for me.

Makla
  • 9,899
  • 16
  • 72
  • 142
  • Your code only retrieves me the first time I initialize the function an output because your code works (for what I'm seeing) for upload..and I'm not uploading anything. I've changed for this `xhrobj.download.addEventListener('progress', function(event)` and `console.log(event)` returns nothing. Plus what code do you have on your PHP file? It's equal to mine? – Linesofcode Mar 17 '14 at 12:37