0

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!

2
  • Ajax is what you want. Ajax stands for asynchronous javascript and xml. Jquery provides a wrapper for pure ajax, but you can run ajax via pure javascript. Take a look at this example. w3schools.com/php/php_ajax_database.asp Commented Jul 15, 2015 at 15:37
  • Thank you for your contribution Commented Jul 15, 2015 at 18:10

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

I noticed that when it enters else { alert("finished !"); } the value of risp is lost (set to false). So if my script would '1' I can't further process that. How would I go about doing something like : i.e. if (risp == '1'){ alert(risp); } }else{ check() }
in your php use echo "finished"; and then use if(risp == "finished"){ .... }else{ check(); } .... or you wanna return a different value from finished??
1

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;

 ?> 

1 Comment

This seems to come closest to what I need, after trying to run it (corrected the typo to javascript) it just keeps requesting function.php : var interval = window.setInterval(function(){ $.ajax({ type: 'POST', url: 'function.php', success: function(response){ if(response == 'true'){ window.clearInterval(interval); } } }); },2000); Any idea what might be causing this ?
1

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

How to use window.setTimeout

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.

1 Comment

Thank you for your contribution

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.