1

if i have something like this in php

$foo=0;
while($foo<20){
echo "hello";
usleep(1000000);
$foo=$foo+1;
}

and i make an ajax request to that php file, can i do anything with the data while the request is in progress? i mean, the script echos hello every second and i saw that the request only shows what data it has when the whole loop is finished, so isnt there a way i can access each hello when its echoed out?

nope
  • 1,002
  • 2
  • 14
  • 31

4 Answers4

0

Look for firebug extension of Firefox.

subsub
  • 1,847
  • 10
  • 21
  • yes i have the extension, im asking if i can somehow collect the hellos when its being echoed. when the request is loading the data if i can just grab hwat it has then. – nope Apr 01 '11 at 20:48
  • no. first off, in PHP, you need to flush the OB buffer to output before the document terminates. second, ajax will not catch flushed fragments anyway and will wait for the end of the data before you get anything useful. you should look at COMET, sockets or for mootools, APE as a solution that can do events and polling. – Dimitar Christoff Apr 01 '11 at 20:52
0

There are a few reasons why you can'y see it.

The content coming from the AJAX request is processed by the server like any other http/php request.

What is happening is the data is being cached by the php buffer, then when its done its flushing it to the output. Which apache then delivers to you.

There is so little data that there is no need to flush this buffer before the processes is done. So you are only seeing the final result.

If you had some much data outputted that it cause the output to be flushed before hand then you may get it.

The other problem is going to be you ajax request handler. I'm pretty sure the onComplete (or similar) method that you (and everyone else) is using will only be called when the output from the server request is finishing and your browser has the full data.

It may be possible to use a different event or perhaps write the ajax code your self (with out using stuff like jQuery) but i'm not even sure if that would solve your problem; as this might also be something to do the with x http request implementation.

May i ask what your are trying to do this for? there may be an easier solution for your actually problem *i'm assuming this isn't the code your actually are using on your site).

Dan

dbers
  • 608
  • 5
  • 16
0

If you execute the flush(); command in PHP you will send content. If you're compressing at the server level you may need to pad output to fill up a packet to make it send.

flush();

Here's an example: http://example.preinheimer.com/flush.php

preinheimer
  • 3,690
  • 18
  • 34
0

The correct answer is you CAN see the content while it's being returned. The other answers were partially correct in mentioning that the PHP output buffer will keep the output "bottled up"... but the output buffer can be disabled.

Once you disable the output buffer you need to show the JQuery response before the request completes - you do this by updating the browser periodically while the connection to the server is still active. This concept is called "Comet" or "Long Polling".

See these questions:

Comet and jQuery

How do I implement basic "Long Polling"?

Comet In PHP

Community
  • 1
  • 1
Ben DeMott
  • 2,294
  • 18
  • 32
  • push solutions don't qualify as ajax. there's no mention of jquery either. just saying. – Dimitar Christoff Apr 02 '11 at 12:28
  • Long-Held Asynchronous connections using XML or JSON can be considered long-polling and still are what I would label generically as 'Ajax'. In fact the accepted answer on the first link is what I thought was useful to the question - to quote "The plugin is an implementation of the Bayeux protocol and currently supports long-polling (local server via AJAX)". I didn't say anything specifically about 'push' - that's what you interpreted from the answer. – Ben DeMott Apr 02 '11 at 23:49