I have an onclick that triggers ajax which calls a php script to pull data from MySQL. This information is then displayed in a div. The problem I am having is that sometimes pulling the data from MySQL takes 2-3 seconds, so the div is empty for about 2-3 seconds. How would I go about adding an animated "Loading" gif to display in the div while it is waiting to display the content?
<!--Ajax Mysql Query-->
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(docId){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('basic-modal-content');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
//var invoice_num = document.getElementById('invoice_num').value;
var invoice_num = document.getElementById(docId).value; // Get previous element ie text input
var queryString = "?invoice_num=" + invoice_num;
ajaxRequest.open("GET", "/invoices/scripts/getattachments.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>