473,557 Members | 2,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3971
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_dat a"] 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_dat a"] 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.updateS tatus('.$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_dat a"] 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.or g/wiki/Comet_%28progra mming%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.updateS tatus('.$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_dat a"] 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******** *************@y 5g2000hsa.googl egroups.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_dat a"] 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.javas cript is a good one.

Sorry about my poor english.
HTH

Apr 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3044
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 what I'm hoping to achieve. I've never before had to use Javascript closures, but now I do, so I'm making an effort to understand them. I've been...
2
1849
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 XMLHttpRequest already got its string from the server... Any help?
0
1824
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 requests to update the content within the filters. For example, I have 2 drop downs, one for state and another for country. When I select a country in...
6
5127
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 application working. However, when attempting to implement the solution, the AJAX calls weren't updating the screen like the examples were and...
2
2027
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. Javascript Codes ------------------------ <script type="text/javascript"> var xmlHttp; function createXMLHttpRequest() ...
10
2110
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 Master pages as their master...) the entire page is refreshed - also the menu which belongs to the master, how can I fix it - so only the inside...
1
3033
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 free $st = mysql_query("SELECT linestate FROM broad WHERE num='40'");
1
4587
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 Database but I'm stuck in the EDIT. I'm getting 2 problems over here. Below is the description: 1)The FIRST page will list all the records from the...
5
4442
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 if I have the entirely wrong approach, but my code is below. Any or all help is appreciated! What currently happens is that the getdivision.php...
0
7549
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7830
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8061
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6180
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5454
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.