Jump to content

Ajax call while php loop running


trix21

Recommended Posts

So basically I have a php loop being called. Something like this (keep it simple).

foreach($item as $i) {
//do some validation stuff
}

That's simple enough.

So what I want to do is update a div while that foreach is going showing which item it's on, kinda like a process update... "1 of 32 complete"... then "2 of 32 complete" etc...

 

I know the basics of making an ajax call but I just can't seem to figure it out... since php is server side and javascript/ajax being client side (browser).

 

Any tips or ideas that could help me out?

Link to comment
Share on other sites

How long does the main php script typically take to execute each pass through the foreach() loop?

 

The ajax cannot make a http request to the same main php script as that would cause another instance of the script to be started. You would need the php script to update the changing status information in a location (database table, flat-file) where another php script (such as get_status.php) could read it and then the ajax would make http requests to the get_status.php page.  You would need to generate a unique process id (perhaps just use the session id if you are already using a session for the visitor) per visitor to identify the correct status information in the http requests so that you could have more than one instance of this process at any point in time.

Link to comment
Share on other sites

Ok, I had to mark unsolved because I can't seem to get it working.

Basically the user would upload a file (csv in this case) and after the upload it does an http request via ajax to run a validation check.

So one http request has already been sent out to let's say process() function in a php script.

That process function is a foreach loop, it seems that once it gets in that loop and ajax is waiting for onreadystatechange I can't do anything... like call another http request to updater() function in another php script that simply calls the database for which line of the csv it's on ("2 of 89 completed") which would update a div block with innerHTML.

After the foreach loop from the first http request is finished ... then it will update the div block with the status from the second http request call.

So I'm stuck on this. I know php is server side... is there a way to update the process div block while the foreach is running?

Link to comment
Share on other sites

No one can actually help you with what your code is doing without seeing all the actual relevant code involved with the problem.

 

113 different people could have written code that is attempting to do what you state and there could be something different wrong in each of their programs but each of them could produce the same symptom as your's.

Link to comment
Share on other sites

 

Ok you're thinking about this all wrong

 

the for loop will be in a php file that is NOT the main php file that generates the page, it is called by JS/Ajax

 

the for loop will not produce any other output apart from either xml/json data

 

the xml/json data is passed back to the js

 

the js then updates the div with what is passed back

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.