2

I have an AJAX call that is running a long PHP script where it has 20 different results, I would like to show when each step in the script is done. Like so 1/20 done, 2/20 done, 3/20 done.

Latest 29-12-2015 03:17.

I tried to create the JSON file as so (jsonFileName_uniqueTimeStampHere.json) by PHP, but the time taken to create the file with PHP, result in a 404 file not found error!

Because when the AJAX call is running it comes to the progress call before the file has been created, I know I can't create the file with JavaScript but is there anyway to create.

The file before the success callback from jQuery AJAX?

What would be the best way to show progress information while AJAX call is running.

The way I have it now, I have a JSON file saved on the server that gets updated with the latest state that has completed, but if multiple users is running the same script the JSON file with the state gets overwritten.

  1. Should I save the state of each progress in DB and then receive it with multiple calls to a PHP method that get state that has been completed?
  2. Should I keep using the current method I use and add a userID to the JSON file so it is unique on each call to the file?
  3. How should I go about doing it the same way as Seositecheckup?

What is the best way to make a progress with AJAX and PHP?

Please tell me if you need any more information.

I have looked around and don't feel like the info or half of info, there is to find online has been enough to do this myself.

I would like to use jQuery AJAX and not XMLHttpRequest, I'm looking for something similar to seositecheckup.com, when you scan a page you can see the state update on each completed function in the console and is done with different AJAX calls. How is that possible?

Should I forget about using jQuery and keep focus on plain JavaScript instead?

Right now I have a setup with jQuery that works the problem is, that I use a JSON file to get the result from and it gets overwritten when multiple users request the same script, is it possible to store the state in db instead and receive it from there with some unique identifier?

In the future I would like to make it possible to put the script into a queue that could be run and when the script ends it should send an e-mail to the user.

Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
user3502250
  • 343
  • 1
  • 5
  • 18
  • Possible duplicate of [What is the cleanest way to get the progress of JQuery ajax request?](http://stackoverflow.com/questions/19126994/what-is-the-cleanest-way-to-get-the-progress-of-jquery-ajax-request) – Matthew Herbst Dec 28 '15 at 20:24
  • Thanks, i looked at the link and its about 2 years old and uses XMLHttpRequest and i would like to use jquery ajax in a similar fashion as seositecheckup. – user3502250 Dec 28 '15 at 20:32
  • jquery ajax is just a wrapper around XHR – Matt Styles Dec 28 '15 at 20:35
  • I do realize that but i dont want to do this using plain javascript i would like to do it with jquery ajax setup, i hope it makes sense. – user3502250 Dec 28 '15 at 20:37
  • It actually makes no sense. if you know a way to accomplish this using vanilla JS why would you constrain yourself to a library that makes this difficult only because you are using it elsewhere? its pointless, but, there seems to be a solution using jQuery within [this answer](http://stackoverflow.com/questions/7740646/jquery-ajax-read-the-stream-incrementally). Basically a hack, but I'd guess it still works. – Matt Styles Dec 28 '15 at 20:40
  • So you would say i should forget about using jquery and keep focus on plain javascript instead Matt? Right now i have a setup with jquery that works the problem is, that i use a json file to get the result from and it gets overwritten when multiple users request the same script, is it possible to store the state in db instead and receive it from there with some unique identifier? – user3502250 Dec 28 '15 at 20:46
  • There is nothing wrong with mixing plain JS and jQuery freely. The purpose of jQuery is to make writing JS easier, but it's not meant to fully replace plain JS. Think about two things when writing code: performance and simplicity (so that you can maintain the code). They often go hand-in-hand. – Matthew Herbst Dec 28 '15 at 20:57
  • So if i should use this with jquery as i do now, how would you go about saving the state in database, and would you recommend it? – user3502250 Dec 28 '15 at 21:01
  • Why not try to break the php function down into mutiple php functions and each php functions echo back the response to an ajax function and the ajax function can give you the success call. – Blkc Dec 29 '15 at 00:46
  • I already have done that, each of the 20 functions has a end result as complete. This get's saved in af json file, but the json file keeps getting overwritten and i'm not sure if its save to use the json file.? – user3502250 Dec 29 '15 at 01:56

1 Answers1

0

The HTTP way of handling requests that may take a long time is for requests to return a 202 and the body of the response should contain the URL where the user can query for the result.

#Request
POST /some/entitities
...

#Response
HTTP/1.0 202 Accepted

/jobs/{jobId}

The user can then poll /jobs/{jobId} which can return a number to represent progress. Do you have to use this? No, but if you do, others developers can immediately recognize what is going on.

Even if you don't use the approach I recommend, you will also have to keep track of job progress in your database and have a separate AJAX call to find out the current progress.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • If i should do this with jquery ajax, how would you save the the data in the databse with a unique identifier that belongs to the user running the script. What would be the best way to create the unique identifier of the progress, can you maybe give me and example of the db build up i have 20 different results. – user3502250 Dec 28 '15 at 20:52
  • You can use guids, you are passing them back to the caller with the 202 – Ruan Mendes Dec 28 '15 at 21:14
  • How would you go about creating it, would you do it with php or in the frontend by javascript? – user3502250 Dec 28 '15 at 21:17
  • @user3502250 the server is the one creating the job, it generates the job id, and the client calls that URL until it is finished, at which point the server will give the client the final URL where the resource can be retrieved. – Ruan Mendes Dec 28 '15 at 21:32
  • So i should just call a method in my class to get the state from db? And delete the row in the table when done? – user3502250 Dec 28 '15 at 21:36