PDA

View Full Version : AJAX: Does Javascript Receive Data From PHP Echo In the Middle of Code?


dealmaker
11-26-2005, 06:02 AM
Hi,
I am writting some AJAX code. Javascript send request using POST, php receives it and echo xml back to javascript. What if I echo xml back in the middle of the php code and then do some maintenance in the same php code afterward? Will Javascript receive the xml before/at the same time that the php maintenance code runs? The reason I ask is that the maintainence code in the php file may take a while to run. I don't want user to wait til it finishes in order to receive the xml, which is exactly what happens if I put the echo statement after the maintenance code.

Many thanks.

ralph l mayo
11-26-2005, 06:10 AM
JavaScript receives PHP code before it renders. That's why they call PHP the preprocessor. The order is PHP code->HTML in Apache->HTML in the client->JavaScript as called from the client html. PHP only receives JavaScript code with a hit to the server, ie a refresh. It's fine to write dynamic JavaScript from PHP but typically not vice versa.

edit: I can't imagine what would really take this much time to run, care to divulge some specifics? Typically the best course is to buffer output and send the completely built page when processing is done, why do you need to do so much to render a page?

dealmaker
11-26-2005, 04:19 PM
I don't think it's the answer of my question. For examples, which one is more responsive to the website visitors, echo xml in the middle of php code and then do a bunch of extra stuff in php code, or echo xml at the end of the php code after doing a bunch of extra stuff in the same php code? In both cases, there is only one php file.

JavaScript receives PHP code before it renders. That's why they call PHP the preprocessor. The order is PHP code->HTML in Apache->HTML in the client->JavaScript as called from the client html. PHP only receives JavaScript code with a hit to the server, ie a refresh. It's fine to write dynamic JavaScript from PHP but typically not vice versa.

edit: I can't imagine what would really take this much time to run, care to divulge some specifics? Typically the best course is to buffer output and send the completely built page when processing is done, why do you need to do so much to render a page?

ralph l mayo
11-26-2005, 04:32 PM
I don't think it's the answer of my question. For examples, which one is more responsive to the website visitors, echo xml in the middle of php code and then do a bunch of extra stuff in php code, or echo xml at the end of the php code after doing a bunch of extra stuff in the same php code? In both cases, there is only one php file.

Yeah, sorry, it doesn't really. Echo the XML as soon as you have enough information to build it completely. Assuming the "extra stuff" doesn't change the XML there's no need to keep the client waiting while PHP builds information that doesn't affect the current display.

dealmaker
11-26-2005, 05:14 PM
But does php care? Does it return data as it go? or does it waits til the script finishes to return data?

Yeah, sorry, it doesn't really. Echo the XML as soon as you have enough information to build it completely. Assuming the "extra stuff" doesn't change the XML there's no need to keep the client waiting while PHP builds information that doesn't affect the current display.

ralph l mayo
11-26-2005, 05:25 PM
But does php care? Does it return data as it go? or does it waits til the script finishes to return data?

It does return data as it goes.


$i = 0;
while (true)
{
++$i;
echo $i;
}


will scroll numbers until you halt execution even though it can never "finish".

Sorry for repeatedly missing your point, hope this helps this time :]

Element
11-26-2005, 10:58 PM
It does return data as it goes.


$i = 0;
while (true)
{
++$i;
echo $i;
}


will scroll numbers until you halt execution even though it can never "finish".

Sorry for repeatedly missing your point, hope this helps this time :]

If you have a fast connection that may not work well for an example, but yes it renders as it goes. Possibly this will show you because its bigger in output.

for($i=1000; $i < 3500; $i++) {
$number = number_format($i);
for($len=$i{3},$r='';strlen($r)<$len;$r.=chr(!mt_rand(0,2)?mt_rand(48,57):(!mt_rand(0,1)?mt_rand(65,90):mt_rand(97,122))));
echo "<p><b>ID: ".$r."</b> : We're running the code again : <b>".$number." time(s)</p>";
}

That will stop at 3,500. It may timeout running through but thats okay, not alot of scrips need to run through that much, I think.

Velox Letum
11-27-2005, 12:59 AM
However if you wrap it in an output buffer, then it won't output until it is complete.

Element
11-27-2005, 02:14 AM
However if you wrap it in an output buffer, then it won't output until it is complete.

It would still work because even though its waiting for it to finnish its still on server-side and the client-side won't load until the server is done doing whatever (PHP/CGI/etc)

dealmaker
11-27-2005, 06:42 AM
ok, now it sounds like you are saying opposite of what all other people are saying in this thread. Is there a typo? Confusing.

It would still work because even though its waiting for it to finnish its still on server-side and the client-side won't load until the server is done doing whatever (PHP/CGI/etc)

brothercake
11-28-2005, 04:00 PM
I think the point here is that it doesn't make any difference.

If you output the code incrementally, or you output it all in one go, the original httprequest will not complete until the output is done, so from a JS perspective it makes no difference - you make one request, you get one response.

If incremental output is what you want, you could perhaps split the PHP into multiple scripts, and do the whole process in several, synchronised requests (ie, response1 outputs then starts request2, and so on)