0

How do I show "loading" text in PHP while code (like a massive database query) is running?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
misctp asdas
  • 973
  • 4
  • 13
  • 35

3 Answers3

3

You can use AJAX calls.

  1. Browser shows "loading text"
  2. Browser processes AJAX request
  3. When the response would come back, "loading text" disappears and you can do something (for example Redirect to somewhere)
s.webbandit
  • 16,332
  • 16
  • 58
  • 82
0

Turn off output buffering and output the html and then start your big code.

apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
//if you are buffering output currently. Output buffering can be nested.
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); } 
ob_implicit_flush(1);

Safari and IE have a 1k buffer, you probably have to fill that before by outputting blanks for example.

Leif
  • 2,143
  • 2
  • 15
  • 26
0

With AJAX you can do it with help of Jquery library.

  <html>
    <head>

       <tittle>Page Tittle</tittle>
      <script type="text/javascript" src="/js/jQuery.min.js"></script>
      function loadingAjax(div_id)  //javascript function
       {  
        $("#"+div_id).html('<img src="ajax-loader.gif"> saving...');  
           // image displayed when the request started
        $.ajax({  
            type: "POST",  
            url: "script.php",        //php script that will return the database result
            data: "name=John&id=28",  
            success: function(msg){  
                $("#"+div_id).html(msg);  //result displayed after success
            }  
        });  
       } 
</head>
<body>
<a href="#" onclick="loadingAjax('myDiv');">save page</a>  
<div id="myDiv"></div>  
</body>

Here the image "ajax_loader.gif" will be displayed till your result from the database is ready.

Hope it helps.

Radheshyam Nayak
  • 1,038
  • 3
  • 11
  • 20