473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
13 3957
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
- 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
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
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
- 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
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
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

"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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
2
by: Alex | last post by:
Example uploaded to: http://www.clickatus.com/ajax/ BTW - This is for FIREFOX, won't work in IE. I don't know why but when it is executed the browser still in loading state... Even though...
0
by: arunprabu | last post by:
Hi, I have a problecm with the AJAX request in my webpage. I have some filters on top of the page. I have a submit button and an empty div below the filters. Some of the filters have ajax...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
2
by: lintolawrance | last post by:
hai friends, i need some help from u.i am trying to work with Ajax.i done some codes for developing dropdownlist using ajax.but it doesn't work.please help me to develop it. ...
10
by: =?Utf-8?B?RGFuaQ==?= | last post by:
Hi, Trying to create a master page that holds a menu, and the menu switches between pages in the site. 2 problem arrosed: a. When I navigate from page to page (all AJAX Web Forms, with the...
1
by: christian | last post by:
Hello I use a AJAX refresh script on a page to test a $var state <div> <? include ("include/refr.inc.php"); //ajax script for reload require ("bd_inc.php"); //test the line state buzy or...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
5
by: thatcollegeguy | last post by:
Below are my 3php and 2js files. I create a table using ajax/php and then want to change the values in the tables add(+ number for teamid) id's for each specific td in the table. I don't know...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.