How to get PHP to update Angular of status of lengthy process?

I have the following Angular site which shows that Angular can update the page while PHP executes some process via AJAX in the background.

However, how can I now get my PHP process, e.g. the for loop to pass back the value of $x so that I can display the follow of $x as it increments?

index.php

<!doctype html>
<html lang="en">
    <head>
      <meta charset="utf-8">
      <title>ajaxtest</title>
      //ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js
    </head>
    <body ng-app="mainApp">
        
Angular is counting: {{counter}}
<button ng-click="processFiles()">processFiles</button>
{{message}}
</div> var mainApp = angular.module('mainApp',[]); function mainController($scope, $interval, $http) { var theTimer = $interval(function () { $scope.counter++; }, 1000); $scope.counter = 0; $scope.message = 'click the button to have PHP do something in the background while Angular should continue to count'; $scope.processFiles = function() { $scope.message = 'processing...'; $http.get('http://localhost/webs/ajaxtest/data.php', { params: { taskIdCode: 'getLoadingStatus' } }).success(function (data) { $scope.message = data['status']; }); } } </body> </html>

data.php

        $taskIdCode = filter_input(INPUT_GET, 'taskIdCode', FILTER_UNSAFE_RAW);
        $methodName = 'task_' . $taskIdCode;
        $this->$methodName();
    }
    public function task_getLoadingStatus() {
        for($x = 1; $x <= 4; $x++) {
            sleep(1);
        }
        $data['status'] = 'finished';
        echo json_encode($data);
    }
}
$taskRunner = new TaskRunner();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s