Ajax

J

jodleren

Hi

I have only used Ajax quite less until now.
But I can see the use of it in a new place, but that is slight
different to what I have done before.

I have a page in PHP, which will open 2 "fields" like: <p>Elapsed
time: <div id="clock"></div></p>.
There is a time, updated by a timer. Easy.

But my PHP script continues to run, and I'd like to send messages such
as "Processed 50%" to the client.
How can I from one page/file/script send messages to my Ajax thing?
As I understand it it asks for a page and waits for the response. Here
I'd like to send "messages" to it.

WBR
Sonnich
 
P

phpCodeHead

Hi

I have only used Ajax quite less until now.
But I can see the use of it in a new place, but that is slight
different to what I have done before.

I have a page in PHP, which will open 2 "fields" like: <p>Elapsed
time: <div id="clock"></div></p>.
There is a time, updated by a timer. Easy.

But my PHP script continues to run, and I'd like to send messages such
as "Processed 50%" to the client.
How can I from one page/file/script send messages to my Ajax thing?
As I understand it it asks for a page and waits for the response. Here
I'd like to send "messages" to it.

WBR
Sonnich

I live on the East side of town and want to go to West side of town by
way of bus. Can you tell me the name of the bus driver?

Huh?

Give a little more code please?

otherwise, help yourself to http://www.w3schools.com/ajax/ajax_intro.asp
 
Á

Álvaro G. Vicario

jodleren escribió:
I have only used Ajax quite less until now.
But I can see the use of it in a new place, but that is slight
different to what I have done before.

I have a page in PHP, which will open 2 "fields" like: <p>Elapsed
time: <div id="clock"></div></p>.
There is a time, updated by a timer. Easy.

But my PHP script continues to run, and I'd like to send messages such
as "Processed 50%" to the client.
How can I from one page/file/script send messages to my Ajax thing?
As I understand it it asks for a page and waits for the response. Here
I'd like to send "messages" to it.

I understand you want to display the progress of a script that takes
quite long to execute. You can't ask this script (if that's your
intention); it's already busy with another HTTP connection. But you can
rewrite it and make it save its status to a place where another script
can read it (e.g., a session variable or a database table). Now you only
need to implement a little fetch-status.php and feed your AJAX app with it.
 
L

lawpoop

Hi

I have only used Ajax quite less until now.
But I can see the use of it in a new place, but that is slight
different to what I have done before.

I have a page in PHP, which will open 2 "fields" like: <p>Elapsed
time: <div id="clock"></div></p>.
There is a time, updated by a timer. Easy.

But my PHP script continues to run, and I'd like to send messages such
as "Processed 50%" to the client.
How can I from one page/file/script send messages to my Ajax thing?
As I understand it it asks for a page and waits for the response. Here
I'd like to send "messages" to it.

WBR
Sonnich

What you might be able to do ( I haven't tested this ) is, in various
places in time.php, you would put
echo "Completed 10%<br>\n";
and then do some more code, and then put
echo "Completed 25%<br>\n";

You might need to use flush() to get these 'updates' to spit out
properly .

Then, at the end of the full page load, use some javascript to delete
the "Completed..." messages.

What you should get is something like
"Loading... 10% Complete
25% Complete
50% Complete
"
etc.
 
T

Tim Streater

jodleren said:
Hi

I have only used Ajax quite less until now.
But I can see the use of it in a new place, but that is slight
different to what I have done before.

I have a page in PHP, which will open 2 "fields" like: <p>Elapsed
time: <div id="clock"></div></p>.
There is a time, updated by a timer. Easy.

But my PHP script continues to run, and I'd like to send messages such
as "Processed 50%" to the client.
How can I from one page/file/script send messages to my Ajax thing?
As I understand it it asks for a page and waits for the response. Here
I'd like to send "messages" to it.

As far as I can tell, it's not possible to do this reliably. Normally at
the JavaScript end, you wait for the readystate of 4 and status of 200,
which tells you that you have received all data that the PHP script sent
out before it quit.

I too would like to be able to do this, but there didn't seem to be a
way not only to receive partial results, but also to ensure that if you
sent a partial result from PHP, it would be exactly that partial item
you received next on the JS side, rather than some fraction of it.

If anyone knows any different then I'd be interested.
 
J

Jorge

(...)
I too would like to be able to do this, but there didn't seem to be a
way not only to receive partial results, but also to ensure that if you
sent a partial result from PHP, it would be exactly that partial item
you received next on the JS side, rather than some fraction of it.

If anyone knows any different then I'd be interested.

What I know is that you can poll xhr.responseText.length at any time
(most meaningful while the transaction is in readystate === 3, because
3 === "receiving"). See:

http://homepage.mac.com/jorgechamorro/cljs/010/index.html

(...)
if (xhr.readyState != 4) {
if (xhr.readyState == 3) {
txt+= " ***** RECEIVING (L: "+xhr.responseText.length+")";
}
}
(...)
 
E

Erwin Moller

Álvaro G. Vicario schreef:
jodleren escribió:

I understand you want to display the progress of a script that takes
quite long to execute. You can't ask this script (if that's your
intention); it's already busy with another HTTP connection. But you can
rewrite it and make it save its status to a place where another script
can read it (e.g., a session variable or a database table). Now you only
need to implement a little fetch-status.php and feed your AJAX app with it.

A small correction:

This is correct, except for the session.
The session is stored at the end of the script, and calls to PHP scripts
with the same sessionid will block until the (long-running) first script
ends.
So write the progress away to a file or a database.
These can be accessed right away. (In case you use a file additional
measures might be needed to avoid you read the value while it is being
written.)

So all in all, a database might be the easiest and safest bet.

Regards,
Erwin Moller




--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
E

Erwin Moller

lawpoop schreef:
What you might be able to do ( I haven't tested this ) is, in various
places in time.php, you would put
echo "Completed 10%<br>\n";
and then do some more code, and then put
echo "Completed 25%<br>\n";

You might need to use flush() to get these 'updates' to spit out
properly .

Then, at the end of the full page load, use some javascript to delete
the "Completed..." messages.

What you should get is something like
"Loading... 10% Complete
25% Complete
50% Complete
"
etc.

Hi,

I tries such approaches in the past and they are not reliable. :-(
After a lot of fiddling I found out that the webserver is sometimes
caching the results even if PHP is flushing.
In some situations it works, in others it doesn't.
Shame though, because it would be the easiest approach. :)

Regards,
Erwin Moller


--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
E

Erwin Moller

Jorge schreef:
What I know is that you can poll xhr.responseText.length at any time
(most meaningful while the transaction is in readystate === 3, because
3 === "receiving"). See:

http://homepage.mac.com/jorgechamorro/cljs/010/index.html

(...)
if (xhr.readyState != 4) {
if (xhr.readyState == 3) {
txt+= " ***** RECEIVING (L: "+xhr.responseText.length+")";
}
}
(...)

But that is NOT a substitute for a timer, Jorge.
It won't tell you how much of the workload the long running script has
processed so far.
The above will only tell you that response from the server is still
coming in.

Regards,
Erwin Moller



--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
J

jodleren

I live on the East side of town and want to go to West side of town by
way of bus. Can you tell me the name of the bus driver?

Huh?

Give a little more code please?

:) well I dont have it yet.

But, say:

<!-- start of my PHP file -->
<?php... <body>
<div id="clock"></div><br>
<div id="userinfo"></div>
.....
<javascript, with time for the clock. I have that, its ok>
<send message to userinfo.innerHTML = 'Starting' HOW?>
..... processing some data
<send message to userinfo.innerHTML = 'Almost done' HOW?>
..... processing some mote data
<send message to userinfo.innerHTML = 'Almost there!' HOW?>
..... processing some mote data
<send message to userinfo.innerHTML = 'Done!'>
foreach(....
<output data as a table or something...>
?><!-- end of my PHP file -->

The problem, as I know java, it will ask for a file, and get the
"answer". The result does not need to be HTML, but just a string...
but it is sent from a file by requesting a "page". That requires 2
pages to make it work.
I have only one page...

WBR
Sonnich
 
J

Jorge

But that is NOT a substitute for a timer, Jorge.
It won't tell you how much of the workload the long running script has
processed so far.
The above will only tell you that response from the server is still
coming in.

My point being that you can monitor incoming data while it's being
received (before readystate === 4, before completion), so, if the
server could push the state updates through that connection... the
client would be able to get them fine...
 
E

Erwin Moller

Jorge schreef:
My point being that you can monitor incoming data while it's being
received (before readystate === 4, before completion), so, if the
server could push the state updates through that connection... the
client would be able to get them fine...

Hi Jorge,

True, but the 'if' is the problem.
I tried this in the past and webservers behave differently when it comes
to delivering the data to the client, even if PHP is flushing it.
But I guess you can maybe finetune this for the webserver in use. I am
unsure how easy or reliable this is.

Regards,
Erwin Moller



--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
J

jodleren

I live on the East side of town and want to go to West side of town by
way of bus. Can you tell me the name of the bus driver?

The name is not important, but a usual busdrive wears a tie and some
sometimes a cap.
That is the proper answer, also for my question.
As for my question, I am asking for an idea, not a solution.
Huh?

Give a little more code please?

No, that is what I asking got.
 
J

jodleren

:) well I dont have it yet.

But, say:

<!-- start of my PHP file -->
<?php... <body>
<div id="clock"></div><br>
<div id="userinfo"></div>
....
<javascript, with time for the clock. I have that, its ok>
<send message to userinfo.innerHTML = 'Starting' HOW?>
.... processing some data
<send message to userinfo.innerHTML = 'Almost done' HOW?>
.... processing some mote data
<send message to userinfo.innerHTML = 'Almost there!' HOW?>
.... processing some mote data
<send message to userinfo.innerHTML = 'Done!'>
foreach(....
<output data as a table or something...>
?><!-- end of my PHP file -->

The problem, as I know java, it will ask for a file, and get the
"answer". The result does not need to be HTML, but just a string...
but it is sent from a file by requesting a "page". That requires 2
pages to make it work.
I have only one page...

WBR
Sonnich

I came up with this idea for <send message to userinfo.innerHTML =
'Starting' HOW?>:

echo "<script type=\"text/javascript\"> document.getElementById
('result').innerHTML='$text'; </script>";
ob_flush();

Not realy AJAX but it seems to do what I need...

WBR
Sonnich
 
J

Jerry Stuckle

jodleren said:
I came up with this idea for <send message to userinfo.innerHTML =
'Starting' HOW?>:

echo "<script type=\"text/javascript\"> document.getElementById
('result').innerHTML='$text'; </script>";
ob_flush();

Not realy AJAX but it seems to do what I need...

WBR
Sonnich

As Erwin pointed out, this in very unreliable. You can flush the PHP
buffers, but not the server's buffers. The only way you can ensure the
data will be sent is at the end of the script.

Also, ob_flush() will do nothing unless you have output buffering on
(i.e. previously called ob_start()).

And BTW - java != javascript! Two entirely different languages who's
names happen to share a few characters.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
(e-mail address removed)
==================
 
C

Christoph Burschka

jodleren said:
Hi

I have only used Ajax quite less until now.
But I can see the use of it in a new place, but that is slight
different to what I have done before.

I have a page in PHP, which will open 2 "fields" like: <p>Elapsed
time: <div id="clock"></div></p>.
There is a time, updated by a timer. Easy.

But my PHP script continues to run, and I'd like to send messages such
as "Processed 50%" to the client.
How can I from one page/file/script send messages to my Ajax thing?
As I understand it it asks for a page and waits for the response. Here
I'd like to send "messages" to it.

WBR
Sonnich

As has been suggested, you can try having the big PHP script set a
$_SESSION variable or write to a file, then have Javascript query a
smaller script that will tell you the status.

But I can tell you that this didn't work on my shared web-server because
the server limited concurrent PHP instances (or concurrent HTTP
connections - or perhaps it was the write lock on the text file). My
status script's execution ended up being delayed until the other script
finished, which defeated the point. So before you invest much time in
this, experiment a bit to make sure your server setup actually supports
the "parallel channel" you need for this.

One thing I haven't tried was writing the status to a static text file
and querying that directly from Javascript, which would eliminate the
need for a second PHP script. The server might still throttle the second
HTTP connection, though.
 
J

Jerry Stuckle

Christoph said:
As has been suggested, you can try having the big PHP script set a
$_SESSION variable or write to a file, then have Javascript query a
smaller script that will tell you the status.

But I can tell you that this didn't work on my shared web-server because
the server limited concurrent PHP instances (or concurrent HTTP
connections - or perhaps it was the write lock on the text file). My
status script's execution ended up being delayed until the other script
finished, which defeated the point. So before you invest much time in
this, experiment a bit to make sure your server setup actually supports
the "parallel channel" you need for this.

One thing I haven't tried was writing the status to a static text file
and querying that directly from Javascript, which would eliminate the
need for a second PHP script. The server might still throttle the second
HTTP connection, though.

This will happen if you use the $_SESSION array. PHP single threads
access to the session.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
(e-mail address removed)
==================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top