1

I'm working on a page to process Excel data.

Currently I have an index page where I once submit JSON data (filename, selected columns, worksheet,..) via $.ajax POST to a PHP page for processing (iterate every row with posted selection). But I would like to have some progress response from the processing page. Because now, it just submits and processes everything in the background without knowing if it's done or not.

Is there some kind of way to:

  1. Redirect to the processing page, along with the JSON POST data, instead of an ajax post?

OR

  1. Return multiple JSON responses from one PHP page (like started, stopped,..) and fetch those responses in the same $.ajax success function? Add some kind of check function like

IF last response-line == started, show image

..after a while (keep checking json response..),

IF last response-line == finished, hide image?

I couldn't use the form submit action, because I'm sending a full JSON string instead of seperate input values.

Am I overlooking something here or is this just not possible (with my way of processing)?

2
  • How does a loader sound? Commented Oct 1, 2015 at 14:43
  • I thought about that, but if it's an excel file with 10k records, it would be nice to see on which row he's currently at instead of 10min loading? Commented Oct 1, 2015 at 14:48

1 Answer 1

3

You need to use ajax calls to a different PHP file that gets the progress from a session. I also did this once and I did it like this:

Javascript

function getProgress(){
    //console.log("progress called");
    $.get( "getprogress.php", function( data ) {
        //console.log(data);
        var p = data.progress;

        if (p!=100){
            //set your progressbar here
        }

    }, "json");
}

And in the function where you start the job you just set a timeout

timeout = setInterval('getProgress()', 1000);

Getprogress.php

ob_start();
session_start();
session_write_close();
$output = array();
$output['progress'] = $_SESSION['progress'];
echo json_encode($output);  

In your PHP file where you do the actual work, you need to set the progress like this:

session_start();
$_SESSION['progress'] = $progress;
session_write_close();

Remember to use session_write_close() because the session can only be open in one process at a time.

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

2 Comments

Genius! I'll take a look at this and give it a shot! Thanks (Probably not the most efficient way, but hey..)
No problem, hope it works for you, I searched a lot at that time and I also believe its not the most efficient way but could not find anything better :)

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.