2

I am using jQuery's ajax to run a php script with the below:

jQuery.ajax({
    type: "POST",
    url: "my_php_script.php",
    success: function( response ) {
        console.log(response);
    },
    error: function(){
        console.log('error is...');
    }
});

my_php_script.php loops through an array and on each product runs a function:

$count = count($array);
$i = 0;
foreach ($array as $item) {
    myFunction($item);
    $i++;
    if($i == $count){
        echo json_encode('all done and a success');
    }
}

This all works but the script can generally take around 2-3 minutes to finish so I need to show the progress for the viewer whilst it runs. How can i do this?

I can get the percent of the loop by adding

$percent = intval($i/$count * 100)."%";
echo json_encode($percent);

into my php loop as so:

$count = count($array);
$i = 0;
foreach ($array as $item) {
    myFunction($item);

    $percent = intval($i/$count * 100)."%";
    echo json_encode($percent);

    $i++;
    if($i == $count){
        echo json_encode('all done and a success');
    }
}

But how can i get this value in my jQuery ajax call?

4
  • you are doing "echo" into the loop. Doesn't it return output to ajax call? Commented Oct 27, 2014 at 10:41
  • At the moment it does but only on success as that's the only time i currently get the response back, I was thinking I need to be able to have a progress callback on the ajax call to show this? Commented Oct 27, 2014 at 10:44
  • You need to use "beforeSend" parameter of ajax calling. See this: dave-bond.com/blog/2010/01/JQuery-ajax-progress-HMTL5 Commented Oct 27, 2014 at 10:49
  • I think beforeSend is now outdatted isn't it? I've tried both examples in the link and it still doesn't show/work Commented Oct 27, 2014 at 11:26

2 Answers 2

1
$.ajax({
    url: path,
    xhrFields: {
        onprogress: function (e) {

        }
    },
    success: function (response) {

    }
});

Found this example hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @wayzz but on trying that code i still can't get the actual progress. The 'loading' state shows before success but not the progress percent - i edited and updated so it should log the precent on progress but i't doesn't: jsfiddle.net/r86gM/68
0

Got this working in the end by writing the percentage in a uniquely named text file as so in my array loop:

$percent = intval($i/$count * 100)."%";
$_text = file_get_contents($_file);
$_text = $percent;
file_put_contents($_file, $_text);

And then in my html page i check the contents of the file every 2 seconds by:

jQuery.get('file.txt?'+new Date(), function(data) {
    console.log(data);
}, 'text');

I'm sure this isn't the best way of doing so but to get this working in a limited time period it's all i could come up with. Hope it helps anyone else

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.