Reputation: 163
I have a main.php page which I want to show a message like : "Checking for completion.."
Main.php will keep calling (without leaving the page) "function.php",
function.php returns with a boolean .
If function.php returns true: I want main.php to write that the operation completed if it returns false, it will need to keep checking untill it returns true.
I am unfamiliar with Ajax/JQuery but from what I understood it might be the best approach. Is someone able to provide me a (working) example of how to accomplish this ?
Thanks!
Upvotes: 0
Views: 373
Reputation: 2408
In summary: You main.php delivers HTML to a webbrowser. In your HTML you write some JavaScript (using XHR/'AJAX') that will start polling the server to check if a certain process is completed.
The script on the server (functions.php) returns true or false to the client. In case of false, you want to check again in, say 5 seconds. Right?
If this is all new to you, you have a lot to dive into, but here are a few pointers:
Good introduction to XMLHttpRequest
If you only want a true or false returnvalue, you don't have to use XML. Just let the server return "true" or "false" as a string.
And last: Make sure your functions.php doesn't need to perform heavy operations to find out if the "process" is finished. If you do, you will put a lot of load on the server during the polling if you have many clients using the page.
Upvotes: 1
Reputation: 2815
main.php
<script type="text/javasctipt">
var interval = window.setInterval(function(){
$.ajax({
type: 'POST',
data: {key1: value1, key2: value2},
url: 'function.php',
success: function(response){
if(response == 'true'){
console.log('Operation Completed'); //<== Completed message use it as you want
window.clearInterval(interval);
}
}
});
},2000);
</script>
Following script will give you a good idea about what you require. I will call function.php every 2 seconds.:
function.php
<?php
//Do you processing
$result = 'true';
echo $result;
?>
Upvotes: 1
Reputation: 5584
Try this
function check(){
return $.ajax({
url: 'function.php',
type:'POST',
success: function(risp){
if(!risp){
check();
}else{
alert("finished!!");
}
},
failure: function(jqXHR, textStatus, errorThrown){
console.log(textStatus);
}
});
}
check();
Upvotes: 1