0

I have this ajax request to update my db.

function ajax_submit(){ var submit_val=$("#stato").serialize(); dest="plan/new_bp1.php"; $.ajax({ type: "POST", url: dest, data: submit_val, success: function(data){ data1=data.split("|"); if(data1[0]=="Successo"){ $("#spnmsg").fadeTo(200,0.1, function(){$(this).removeClass().addClass("spn_success").html(data1[1]).fadeTo(900,1)}); }else if(data1[0]=="Errore"){ $("#spnmsg").fadeTo(200,0.1, function(){$(this).removeClass().addClass("spn_error").html(data1[1]).fadeTo(900,1)}); } }, complete: function(){ setTimeout(function(){ $('.container').load('plan/home.php');},2000); } }); } 

The called script will take long to perform since it has to select, elaborate and insert around 4.000 recors each time. What I do now is to add a spinner on the screen to give users a feedback that the request is working (triggered by AjaxStart and AjaxStop).

At the end of the call the complete function will echo what the php script will echo.

What I'd like to do is to have a counter that will update during the script execution where I can say something like "X records out of 4.000 processed" On the script side I have no problem to calculate the number of processed records and the number of record overall. How can I update my script to show the progress?

4
  • You store the completed actions is a file or db, and then do another ajax request to poll that file or db, and get the result as the first PHP script is working Commented Jan 10, 2016 at 18:38
  • there is no option for the current request to receive a feedback echoed by the php script? Commented Jan 10, 2016 at 18:41
  • Not really, you have to push (SSE or Sockets) the progress to the client, or just poll for it with ajax, and you have to do it with another ajax call, as you can't return partials with ajax, and still keep the job going etc. It's generally harder than one first assumes, you can't just stick on jQuery's progress handler for something like this. Commented Jan 10, 2016 at 18:44
  • got it. I hoped to miss something on progress that could help me ;) Commented Jan 10, 2016 at 18:47

1 Answer 1

2

You have a couple of options.

1) Right when the request starts you can start polling a different endpoint that just serves out the number of records that have been processed, like @adeneo suggested. You don't have to write to a file every time a record is processed. You can just store it in memory and ensure that the route handler you have to handle requests coming in for the progress has access to that same memory.

2) Implement a websocket endpoint on your server that pushes out the number of processed records. Basically, on the server then, you will call that Websocket library code to push out the progress. You will also have to create a Websocket connection on the Javascript side, which is trivial, and listen for those messages.

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

1 Comment

Regarding point 1. it should be noted that PHP sessions have issues with multiple ajax requests accessing the same session data, so that's generally not an option, and can cause a lot of grief (been there);