I’m trying to make a loading screen for my webapp. I’m using PHP backend and calling a DB query. The fact is, that I expect it to take up to 5 seconds (which is fine). But I’d like to show a loading screen in the meantime and I’ve found myself somehow struggling with it.
I 100% knew this wouldn’t work, but just for illustration of what I need to achieve, I tried this:
<div id='loading'>i am loading</div>
<?php
$result = $db->query("some sql here");
if ($result)
echo "<script>document.getElementById('loading').style.display = 'none';</script>";
?>
Although this piece of code is obviously wrong, I hope it is clear from it what I need. NOTE: I cannot really afford redirecting to another page for the loading screen (I saw this as one of the options), unless it is the only possible solution like this. Is there another way of doing this?
2
Answers
I think you can use these codes:
But this method your request with AJAX. You can open your loading div first of all. And you request query in another URL and you can post/get (http methods) URL and when query ended AJAX completed and then you can hide your loading div.
Edit: i removed ajax request when press button code.
$("#button").click(function () { AJAX CODE HERE })
;You can use an XMLHttpRequest(), I won’t be writing the HTML for this answer.
Then in PHP