If you are looking to learn how to create a loading spinner or put a loading icon when there is ajax request or form submission then you are in right place.
Why need loading spinner ?
CSS
Here, This CSS is not mandatory but if you keep it will make user un clickable to page.
th{ color: white; }
.modal-busy {
position: fixed;
z-index: 999;
height: 100%;
width: 100%;
top: 0;
left: 0;
background-color: Black;
filter: alpha(opacity=60);
opacity: 0.6;
-moz-opacity: 0.8;
}
.center-busy {
z-index: 1000;
margin: 300px auto;
padding: 0px;
width: 130px;
filter: alpha(opacity=100);
opacity: 1;
-moz-opacity: 1;
}
.center-busy img {
height: 128px;
width: 128px;
}
<div> making unclickable
<div class="modal-busy" id="loader" style="display: none">
<div class="center-busy" id="test-git">
<img alt="" src="ajax-loading.gif" />
</div>
</div>
ajax.js
This code fetches the records from the server using AJAX request, and responsible to show loader icon until data is not fully loaded.
$(document).ready(function () {
$("#loader").show();
$.post("script.php",{api:"getData"},function (response) {
var data = response.split('^');
$("#tbody").html(data[1]);
$("#loader").hide();
});
});
script.php
This code the actual logic that fetches the records from the database by SQL query.
<?php
include "config.php";
include_once "Common.php";
if (isset($_POST['api']) == "getData") {
$sr = 1;
$tableData = '';
$common = new Common();
$data = $common->getData($connection);
if ($data->num_rows > 0 ){
while ($row = $data->fetch_object()) {
$tableData .= '<tr>
<td>'.$sr.'</td>
<td>'.$row->name.'</td>
<td>'.$row->class.'</td>
<td>'.$row->marks.'</td>
<td>'.$row->gender.'</td>
</tr>';
$sr++;
}
echo 'test^'.$tableData;
}
}
This file includes config.php & Common.php. The code is given below.
Config.php
<?php
$connection = new mysqli("localhost","root","","codingbirds");
if (!$connection) {
die("Error in database connection". $connection->connect_error);
}
?>
Common.php
<?php
class Common
{
public function getData($connection) {
$query = "SELECT * FROM bird_records";
$result = $connection->query($query);
return $result;
}
}
I have given the code in parts so that I can make you understand. Now here is the complete code
Complete Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<link rel="icon" href="https://codingbirdsonline.com/wp-content/
uploads/2019/12/cropped-coding-birds-favicon-2-1-192x192.png"
type="image/x-icon">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/
3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/
3.4.1/jquery.min.js"</script>
<script src="ajax.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/
bootstrap.min.js"></script>
<style>
th{ color: white; }
.modal-busy {
position: fixed;
z-index: 999;
height: 100%;
width: 100%;
top: 0;
left: 0;
background-color: Black;
filter: alpha(opacity=60);
opacity: 0.6;
-moz-opacity: 0.8;
}
.center-busy {
z-index: 1000;
margin: 300px auto;
padding: 0px;
width: 130px;
filter: alpha(opacity=100);
opacity: 1;
-moz-opacity: 1;
}
.center-busy img {
height: 128px;
width: 128px;
}
</style>
</head>
<body>
<div class="container" style="margin-top: 50px;">
<center>
<img width="50" src="https://codingbirdsonline.com/wp-content/
uploads/2019/12/cropped-coding-birds-favicon-2-1-192x192.png"/>
</center>
<h2>Create loading spinner indicator in jquery ajax,
while data loads</h2>
<p>How to create loading spinner indicator in jquery ajax</p>
<table class="table">
<thead>
<tr style="background-color: #1573ff">
<th>Sr.No</th>
<th>Name</th>
<th>Class</th>
<th>Marks</th>
<th>Gender</th>
</tr>
</thead>
<tbody id="tbody"></tbody>
</table>
</div>
</body>
<div class="modal-busy" id="loader" style="display: none">
<div class="center-busy" id="test-git">
<img alt="" src="ajax-loading.gif" />
</div>
</div>
</html>
I hope you got how to do this. If you face any error or missing any steps then you can download the source code from here.
Thanks for visiting Happy Coding 🙂