-1

Is it possible to get multiple callbacks using jQuery .post for a ajax upload of an image to php?
I am sending an image to the server for processing thru binary string and would like to hav the front end display status when the image has uploaded then a processing icon.

Currently I am using the standard .post but ideally this is the scenario i am looking for -
jquery:

$.post( 'process.php', { myvar:myvar}, function(data){

    if(data=='callback_1'){
        // process 1
    }else if(data=='callback_2'){
        // process 2
    }

}); 

php:

$myvar = $_POST['myvar'];

// after process 1
echo 'callback_1'

// after process 2
echo 'callback_2'
t q
  • 4,593
  • 8
  • 56
  • 91
  • You might check out the source for the [jQuery file upload](http://blueimp.github.io/jQuery-File-Upload/) plugin, which does exactly what you're asking. – Austen Aug 09 '13 at 22:28
  • @Austen ive seen this but couldnt get it to work properly on firefox, chrome. i get an error icon under the image title on the left – t q Aug 09 '13 at 22:31
  • 1
    That's too bad. That plugin has saved me a lot of trouble. Perhaps another SO question could solve your problem ;) – Austen Aug 09 '13 at 22:35

1 Answers1

0

I set an array in the php script

$r['callback_one'] = 'complete';
$r['callback_two'] = 'fail';

echo json.encode($r);

javascript:

r = JSON.parse(data);
if (r.callback_one === 'complete){//do something};

of course this still sends the callbacks all at once, which may not be what you are after.


the only thing i can think of if you need to try and get things sequentially is to use flushing:

How to flush output after each `echo` call?

ob_end_flush();

# CODE THAT NEEDS IMMEDIATE FLUSHING

ob_start();

What may even be happening is that the client does not receive the packet from the server until the server has built up enough characters to send what it considers a packet worth sending.

Old Answer:

You could use ob_implicit_flush which will tell output buffering to turn off buffering for a while:

ob_implicit_flush(true);

# CODE THAT NEEDS IMMEDIATE FLUSHING

ob_implicit_flush(false);
Community
  • 1
  • 1
Smith Smithy
  • 585
  • 6
  • 24