-1

I have a form and its action triggers the loading of data from a table. I want the action to return and display a loading gif while the query is still running.

php:

if(isset($_POST['button']))
{
    echo '<div id="loading"></div>';
    for($x=0; $x<10000; $x++)
    {
        $conn->query("SELECT * FROM table");
    }

}

html:

My current code doesn't show the loading gif until the query has already run.

7
  • 2
    Why do you want to run your SELECT statement 10000 times? Commented May 18, 2015 at 13:18
  • 4
    You miss some basics here: PHP is first process by your server, then the result is sent to your browser when all your script is processed. You need to check on AJAX for what you want to do. Commented May 18, 2015 at 13:19
  • What about outputting the page then populating the page with the data via AJAX? learn.jquery.com/ajax Commented May 18, 2015 at 13:19
  • You need AJAX to do this kind of stuff. learn JavaScript and jQuery. In your JavaScript code, hide the loader at the beginning. then only show it when you make an AJAX request to the server. Commented May 18, 2015 at 13:27
  • can you give me some links about that.. ? Commented May 18, 2015 at 13:30

1 Answer 1

0

In order for you to display a loading image in the browser while your SQL query is running, you need to separate the displaying of the loading image from executing the query. The way you have it right now, php will not return your loading '' until the query completes.

The simplest way is for the loading to be initiated via javascript, which would inject the loading image into your html and fire off an AJAX request to the server. Then when the AJAX call returns the data, the receiving javascript callback would replace the loading image with your data.

Some similar questions that can get you on your way:

Load data from database + ajax + php

Loading data from database with php and pure ajax in a textbox

How to show ajax loading gif animation while the page is loading?

Sign up to request clarification or add additional context in comments.

Comments

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.