1

I have a run.php that has a button on it called "Run". Clicking it does a jQuery $.get() AJAX call back to a response.php on the server, and sends the result to a Javascript callback function getResponse(sData) inside run.php's HTML for display back on the browser. I want to have things such that as response.php runs through a list of tasks, it echoes a response back with the echo command and for getResponse to update a DIV with that status as it moves along. So, let's say I have 5 steps inside response.php, and therefore 5 echo statements back to getResponse().

I tried to get this to work, but what happens is that jQuery waits and then sends one single response all at once, rather than sending as it goes along with the 5 responses.

What's the technique?

The reason I ask is that I have a script that does something to a bunch of files. The first thing it does is a file count, so it updates my progress bar. Then, as it runs through files, it needs to increment my progress bar like every 1000 files.

2
  • Did you try to send ajax requests in a loop? Commented Jan 23, 2010 at 7:21
  • Problem there is that response.php takes a long while to run. It starts by counting files that average around 300,000. It then reads them into a database. While it does this, the web page needs to update a progress bar. Commented Jan 23, 2010 at 7:25

2 Answers 2

1

I think there's no way to make that ajax call to have multiple response in just one call... but what I could suggest is you make a session on php... and in every steps on your tasks function, update that session... then make another ajax call that checks that session if any updates happened... if there is update then do what you have to do....

Sign up to request clarification or add additional context in comments.

6 Comments

You're clear, it's just that response.php has to run a process against 300,000 files and takes a long time to run. I would have to make response.php work only against like 1000 files at a time, which isn't efficient but would work.
Ah! I may have it. The response.php runs, but updates a session var. Another script, check.php could be used for checking the session var periodically. Is that what you mean?
yes... that's what I mean... and that check.php will do the update for your progress bar...
The strangest thing. Blocking is occurring. The check.php is returning an empty session var until the final request.php finishes. Got a fix?
Did you get this to work? I'm having the same problem, anyone have sample code to create the php session etc?
|
0

As you can't really get progress with xmlhttprequest, I suggest you can look into other ways of doing AJAX calls. One of them is through iframe. You can create hidden iframe, set it's sources to request.php and then periodically just check it's content. It should be possible since it's all it the same domain and restrictions does not apply.

iframe might work because it's not that different from normal browser window, meaning that it periodically applies data it gets into DOM even if request hasn't been finished yet. There's potentially might be problems with how different browsers do that, i.e. IE shows new content only if it got more than 4K or something. But it is possible to overcome that, I'm sure.

So, create new hidden iframe, add src attibute to your php script, make that script periodically write something to the client and on the client check what have been written and convert it to shiny GUI stuff.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.