By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,559 Members | 2,992 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,559 IT Pros & Developers. It's quick & easy.

Using AJAX, how can the page get state infomation from backgroud PHP script?

P: n/a
Hi,

I'm not familiar with web programming, but I have a problem here.

I have a page. When a user click one button on it, I will use AJAX to
request a PHP script which will do a bunch of tasks, asynchronously.
These tasks might take long time so I want to keep the user informed
of the progress. The problem is that only the PHP script knows the
progress, how can the web page gets these information from PHP script?

I'd really appreciated if someone could help me solve this problem.

Apr 13 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Marvin Zhang wrote:
Hi,

I'm not familiar with web programming, but I have a problem here.

I have a page. When a user click one button on it, I will use AJAX to
request a PHP script which will do a bunch of tasks, asynchronously.
These tasks might take long time so I want to keep the user informed
of the progress. The problem is that only the PHP script knows the
progress, how can the web page gets these information from PHP script?

I'd really appreciated if someone could help me solve this problem.
Just an idea, someone who's done it may know better.

- Have your original PHP store it's "status" information in a Session
variable says $_SESSION["status_data"] which is updates as it runs

- Have a timer on the Javascript side which creates a 2nd Ajax call
to a new getstatus.php, which then reads the status from the session and
sends back to the browser in this 2nd Ajax call.

Not done much Ajax, I am assuming you can make another ajax call whilst
one is currently running??

You might need to send the PHP session id back to the server when you
call getstatus.php, not sure.

Let us no how you do :)
Apr 13 '07 #2

P: n/a
- Have your original PHP store it's "status" information in a Session
variable says $_SESSION["status_data"] which is updates as it runs

- Have a timer on the Javascript side which creates a 2nd Ajax call
to a new getstatus.php, which then reads the status from the session and
sends back to the browser in this 2nd Ajax call.

Not done much Ajax, I am assuming you can make another ajax call whilst
one is currently running??

Wow, that's really clever actually. The only think I'm not sure about
is when first script actually saves the session data - I don't know if
it gets saved as it's set, or if it saves it when the script
completes. Yes, you can definitely send multiple ajax requests. I
think what will happen if the script runs really long is that the
first one might time out - I'm really not sure. But you should
definitely be able to get the process going with one ajax call, then
poll the second script for the status. Yeah, I'd love to hear how
this works out as well...

Aerik

Apr 13 '07 #3

P: n/a
Marvin Zhang wrote:
Hi,

I'm not familiar with web programming, but I have a problem here.

I have a page. When a user click one button on it, I will use AJAX to
request a PHP script which will do a bunch of tasks, asynchronously.
These tasks might take long time so I want to keep the user informed
of the progress. The problem is that only the PHP script knows the
progress, how can the web page gets these information from PHP script?

I'd really appreciated if someone could help me solve this problem.
The suggested 2nd AJAX call is by far the most elegant solution I'd say.
From when I never even heard of AJAX I found a 'faked' workaround for
user feedback during a long wait:

I found a script to make animated gifs on the fly, drew a progress bar
with several separately saved frames, and used that to set the frame
delay to a custom value based on a rough guesstimate on the server of
the total execution time of the loop or whatever caused a long delay for
the user.

Of course with JavaScript this could be handled much more gracefully.

Sh.
Apr 13 '07 #4

P: n/a
On Apr 13, 5:25 am, "Marvin Zhang" <zyem...@gmail.comwrote:
Hi,

I'm not familiar with web programming, but I have a problem here.

I have a page. When a user click one button on it, I will use AJAX to
request a PHP script which will do a bunch of tasks, asynchronously.
These tasks might take long time so I want to keep the user informed
of the progress. The problem is that only the PHP script knows the
progress, how can the web page gets these information from PHP script?

I'd really appreciated if someone could help me solve this problem.
You might want to look at Comet http://en.wikipedia.org/wiki/Comet_%28programming%29
Comet is a programming technique that enables web servers to send data
to the client without having any need for the client to request it. It
allows creation of event-driven web applications which are hosted in
the browser.

Another solution is to not use AJAX, but a hidden iframe, load your
page into the iframe and while the php is running make it output
javascript that will update the status on the main page.

Example:

for($x = 0; $x < 10; $x++) {
echo '<script>window.parent.updateStatus('.$x.');</script>';
flush();
}
Check here for more info on iframe remote scripting
http://developer.apple.com/internet/...nt/iframe.html

Apr 13 '07 #5

P: n/a
- Have your original PHP store it's "status" information in a Session
variable says $_SESSION["status_data"] which is updates as it runs

- Have a timer on the Javascript side which creates a 2nd Ajax call
to a new getstatus.php, which then reads the status from the session and
sends back to the browser in this 2nd Ajax call.

Not done much Ajax, I am assuming you can make another ajax call whilst
one is currently running??

You might need to send the PHP session id back to the server when you
call getstatus.php, not sure.

Let us no how you do :)
Thanks for you idea. I believe this should work. I'll try next week
and give result here.

Apr 14 '07 #6

P: n/a
You might want to look at Comethttp://en.wikipedia.org/wiki/Comet_%28programming%29
Comet is a programming technique that enables web servers to send data
to the client without having any need for the client to request it. It
allows creation of event-driven web applications which are hosted in
the browser.

Another solution is to not use AJAX, but a hidden iframe, load your
page into the iframe and while the php is running make it output
javascript that will update the status on the main page.

Example:

for($x = 0; $x < 10; $x++) {
echo '<script>window.parent.updateStatus('.$x.');</script>';
flush();

}

Check here for more info on iframe remote scripting
http://developer.apple.com/internet/...nt/iframe.html
Thanks for your feedback. Actually I never heard of Comet, and know
little about iframe. It seems Comet is a great tool. I'll look into it.

Apr 14 '07 #7

P: n/a
Just an idea, someone who's done it may know better.
>
- Have your original PHP store it's "status" information in a Session
variable says $_SESSION["status_data"] which is updates as it runs

- Have a timer on the Javascript side which creates a 2nd Ajax call
to a new getstatus.php, which then reads the status from the session and
sends back to the browser in this 2nd Ajax call.

Not done much Ajax, I am assuming you can make another ajax call whilst
one is currently running??

You might need to send the PHP session id back to the server when you
call getstatus.php, not sure.

Let us no how you do :)
I've tried out this approach. But unfortunately, there's a problem
prevent this solution from working. If we send more than one request
to the same session, all the requests will be queued. The second
request will be send only after the response of the first request is
received. So it's impossible to poll the information with second AJAX
request.

I'm planning to use a table in the database to record process
information. The PHP script at the server side will update the record
in the table and the web page sends request regularly to poll the
information from database, just the same as you suggested. This will
work cause we don't need to use session here. Using database, not only
PHP script but also the daemon written in C++ can update that
infomantion, this is what I need.

Anyway, thanks for you original idea. That really helps very much :)

Apr 18 '07 #8

P: n/a

"Marvin Zhang" <zy*****@gmail.comschreef in bericht
news:11*********************@y5g2000hsa.googlegrou ps.com...
>Just an idea, someone who's done it may know better.

- Have your original PHP store it's "status" information in a Session
variable says $_SESSION["status_data"] which is updates as it runs

- Have a timer on the Javascript side which creates a 2nd Ajax call
to a new getstatus.php, which then reads the status from the session and
sends back to the browser in this 2nd Ajax call.

Not done much Ajax, I am assuming you can make another ajax call whilst
one is currently running??

You might need to send the PHP session id back to the server when you
call getstatus.php, not sure.

Let us no how you do :)

I've tried out this approach. But unfortunately, there's a problem
prevent this solution from working. If we send more than one request
to the same session, all the requests will be queued. The second
request will be send only after the response of the first request is
received. So it's impossible to poll the information with second AJAX
request.

I'm planning to use a table in the database to record process
information. The PHP script at the server side will update the record
in the table and the web page sends request regularly to poll the
information from database, just the same as you suggested. This will
work cause we don't need to use session here. Using database, not only
PHP script but also the daemon written in C++ can update that
infomantion, this is what I need.

Anyway, thanks for you original idea. That really helps very much :)
Hi Marvin,

I'm not sure if this will help but just some brainstorming here:

The XMLHttpRequest object for using AJAX stuff has a property 'readyState'
which can be:

0 // Uninitialized
1 // Loading
2 // Loaded
3 // Interactive
4 // Finished

If I were to hack something you have in mind I would look at Loading and
Interactive (not sure what the latter does)

My guess is that if you address one of these while the page is loading and
have PHP flush the buffer once in a while you could catch an XML node called
'loadedSoFar' or any output for that matter to poll the state of the page.

I recently saw a little AJAX script that didn't recieve XML at all but
simply some javascript statements where on the client javascript did an
eval( output ) on the recieved input. Pretty ingenius IMHO.

Anyway, hope this gives you some pointers.

Cheers.

Apr 18 '07 #9

P: n/a
On Apr 18, 9:57 am, Marvin Zhang <zyem...@gmail.comwrote:
>
I've tried out this approach. But unfortunately, there's a problem
prevent this solution from working. If we send more than one request
to the same session, all the requests will be queued. The second
request will be send only after the response of the first request is
received. So it's impossible to poll the information with second AJAX
request.
I have ever seen javascript library who can do 2 request to server and
javascript handles both response. The one come first executed first,
don't care the ordering of the request.
If you stuck with ajax, post your problem to javascript newsgroups.
comp.lang.javascript is a good one.

Sorry about my poor english.
HTH

Apr 18 '07 #10

P: n/a
Marvin Zhang wrote:
>Just an idea, someone who's done it may know better.

- Have your original PHP store it's "status" information in a Session
variable says $_SESSION["status_data"] which is updates as it runs

- Have a timer on the Javascript side which creates a 2nd Ajax call
to a new getstatus.php, which then reads the status from the session and
sends back to the browser in this 2nd Ajax call.

Not done much Ajax, I am assuming you can make another ajax call whilst
one is currently running??

You might need to send the PHP session id back to the server when you
call getstatus.php, not sure.

Let us no how you do :)

I've tried out this approach. But unfortunately, there's a problem
prevent this solution from working. If we send more than one request
to the same session, all the requests will be queued. The second
request will be send only after the response of the first request is
received. So it's impossible to poll the information with second AJAX
request.

I'm planning to use a table in the database to record process
information. The PHP script at the server side will update the record
in the table and the web page sends request regularly to poll the
information from database, just the same as you suggested. This will
work cause we don't need to use session here. Using database, not only
PHP script but also the daemon written in C++ can update that
infomantion, this is what I need.

Anyway, thanks for you original idea. That really helps very much :)
Yes, your problem is PHP serializes access to session data. For the
second request the be processed, the first request must either complete
or otherwise close the session (i.e. session_write_close()).

But if you close the session you can't reopen it in your long-running
script.

Your database idea should work great.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 18 '07 #11

P: n/a
The XMLHttpRequest object for using AJAX stuff has a property 'readyState'
which can be:

0 // Uninitialized
1 // Loading
2 // Loaded
3 // Interactive
4 // Finished

If I were to hack something you have in mind I would look at Loading and
Interactive (not sure what the latter does)

My guess is that if you address one of these while the page is loading and
have PHP flush the buffer once in a while you could catch an XML node called
'loadedSoFar' or any output for that matter to poll the state of the page.
Hi,

Thank you for you reply.

I tried this method. Under Firefox, it works great. Every time the
server script calls flush(), the callback function of Ajax request
will be called with readyState set to 3. So I can update the progress
with the responseText variable. But unfortunately, this doesn't work
in IE. In IE, the callback will only be called only once with
readyState set to 3.

I hate IE!!
Apr 19 '07 #12

P: n/a

"Marvin Zhang" <zy*****@gmail.comschreef in bericht
news:11**********************@e65g2000hsc.googlegr oups.com...
>The XMLHttpRequest object for using AJAX stuff has a property
'readyState'
which can be:

0 // Uninitialized
1 // Loading
2 // Loaded
3 // Interactive
4 // Finished

If I were to hack something you have in mind I would look at Loading and
Interactive (not sure what the latter does)

My guess is that if you address one of these while the page is loading
and
have PHP flush the buffer once in a while you could catch an XML node
called
'loadedSoFar' or any output for that matter to poll the state of the
page.

Hi,

Thank you for you reply.

I tried this method. Under Firefox, it works great. Every time the
server script calls flush(), the callback function of Ajax request
will be called with readyState set to 3. So I can update the progress
with the responseText variable. But unfortunately, this doesn't work
in IE. In IE, the callback will only be called only once with
readyState set to 3.
Alright, maybe the following will be a big resource hog on the client:
But what about (in IE that is) a javascript loop to poll responseText, say
every 500ms, for as long as readyState is 3 and hasn't changed?

By the way, I'm curious: how is your alternative coming along?

Cause what I didn't quite understand is how your suggested alternative
differs from the initial problem. You said you would poll the database now,
in stead of a session var right? But how are you gonna do that without a
second AJAX object?

Cheers
Apr 19 '07 #13

P: n/a
On Apr 18, 3:14 am, Bocah Sableng <cahsabl...@gmail.comwrote:
On Apr 18, 9:57 am, Marvin Zhang <zyem...@gmail.comwrote:
I've tried out this approach. But unfortunately, there's a problem
prevent this solution from working. If we send more than one request
to the same session, all the requests will be queued. The second
request will be send only after the response of the first request is
received. So it's impossible to poll the information with second AJAX
request.

I have ever seen javascript library who can do 2 request to server and
javascript handles both response. The one come first executed first,
don't care the ordering of the request.
If you stuck with ajax, post your problem to javascript newsgroups.
comp.lang.javascript is a good one.

Sorry about my poor english.
HTH
and you can send more than one request using lib. provided by
ajaxtoolbox.com.
it is free. Examples are there.

Apr 20 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.