0

I've got the following function that calls a php file which returns a percentage of a task thats complete.

Can anyone tell me what kind of data do i need to put in the PHP file to pass the percentage to the ajax call and how do i display the percentage on the page? e.g like a jQuery progress bar?

Thanks

function getStatus() {
    //check your progress
    $.ajax(
            {
                type: 'GET',
                url: "checkProgress.php",
                async: true,
                success:
                    function (data) {
                        //assume the data returned in the percentage complete
                        var percentage = parseInt(data);

                        //write your status somewhere, like a jQuery progress bar?

                        if (percentage < 100) {
                            //if not complete, check again
                            getStatus();
                        }
                    }
            });
}  
John
  • 6,417
  • 9
  • 27
  • 32
  • 1
    Can you explain the task you are doing in the server so we might be able to help you. By the way I think your approach is good at this point. – DARK_DUCK Jan 12 '14 at 13:15
  • pass the value in the PHP by JSON or plaintext. shouldn't matter. I would personally pass it by json, so I can extend the description of the progress. – Gasim Jan 12 '14 at 13:21
  • DARK_DUCK, basically the php file is going to be generating lots of pdf files and storing them on the server. but the quantity of pdf could be in the hundreds, as the file is doing them it will store a temp file to know what file number it is on and calculate a percentage for that. – John Jan 12 '14 at 13:38

1 Answers1

0

Every time you receive a request at your PHP file, you'll have to deal with the PHP HTTP Request variables (including $_REQUEST, $_GET & $_POST)

That's if you're dealing with data, but as you're just doing it to gain a response, you could just use this:


I don't know how your back-end is sorted (how you check progress), but you could do something like this:

#checkProgress.php
<?php
    return //your progress logic
?>

This will allow you to handle the response with your JS, which should come through in the success callback (as you've got already):

$.ajax(
            {
                type: 'GET',
                url: "checkProgress.php",
                async: true,
                success:
                    function (data) {
                        //assume the data returned in the percentage complete
                        var percentage = parseInt(data);

                        //write your status somewhere, like a jQuery progress bar?

                        if (percentage < 100) {
                            //if not complete, check again
                            getStatus();
                        }
                    }
            });
Richard Peck
  • 76,116
  • 9
  • 93
  • 147