Use Ajax to show progress PHP script triggered by another Ajax request
I am making a jQuery Ajax call to a PHP script that has been running for a sufficient amount of time (1 to 3 minutes). It constantly logs the percentage of completion in the database. How can I continually fire another Ajax request to tell the percentage of the MySQL database to the user about it?
EDIT I understand how to use a separate PHP script to query the database, so my question is how to set up the JavaScript and Ajax calls
source to share
After the AJAX call to start the process, you can make another AJAX call in a loop that requests, returns, and passes the current percentage until it reaches 100%. Basically, one AJAX call to initiate a process, followed by a series of calls that check the status.
Update: Here's some simple JavaScript to achieve what you want:
<script>
function startProcess() {
//start your long-running process
$.ajax(
{
type: 'GET',
url: "/longRunningProcess",
async: true,
success:
function (data) {
//do something - your long process is finished
}
});
}
function getStatus() {
//check your progress
$.ajax(
{
type: 'GET',
url: "/checkProgress",
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();
}
}
});
}
source to share
You will probably need another URL to call the poll results from the database. In other words, another PHP script that reports progress. The Ajax call will probably use a timeout or any other fancy stuff that jQuery has to schedule on its own (until the server call reports are complete).
Remember to handle exceptional cases where server side processing dies (and thus never finishes!)
source to share