Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
This is my first php project. I have imlemented partial ajax postback by referring to this article of PHP AJAX MYSQL from W3Schools

Now, I am trying to show a loading gif when the partial loading starts and hide it when the partial loading completes. Here is the code that I am using in Javascript:

<pre lang="Javascript">function showUser1(str) 
    {
        if (str == "") 
        {
            document.getElementById("mems").innerHTML = "I caanot fetch: " + str;
            return;
        }
        else 
        { 
            document.getElementById("loadgif").style.visibility= "visible";
            if (window.XMLHttpRequest) 
            {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else 
            {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() 
            {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
                {
                    document.getElementById("outerpicwrapper").innerHTML = "";
                    document.getElementById("mems").innerHTML = xmlhttp.responseText;
                }
            };
            xmlhttp.open("GET","getmembers.php?q2="+str,true);
            xmlhttp.send();
            document.getElementById("loadgif").style.visibility= "hidden";
        }
    }

In the first line of body, I have written:

HTML
<img id="loadgif" src="img/loading.gif" 
class="loadinggif" />
In the CSS file, i have written:

CSS
.loadinggif
{
position: absolute;
z-index: 200;
visibility: hidden;
}

The code is working fine and shows the data but, loading gif is not shown. I have even tried display:none and display:block in place of visibility. Kindly help.

What I have tried:

visibility: hidden
display: none
Posted
Updated 2-Aug-16 5:11am

1 solution

You're hiding the element again before the request has completed. Move the visibility = "hidden" line inside the if block in the onreadystatechange callback:
JavaScript
xmlhttp.onreadystatechange = function() 
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
    {
        document.getElementById("outerpicwrapper").innerHTML = "";
        document.getElementById("mems").innerHTML = xmlhttp.responseText;
        
        // Add here:
        document.getElementById("loadgif").style.visibility= "hidden";
    }
};

xmlhttp.open("GET","getmembers.php?q2="+str,true);
xmlhttp.send();

// Remove here:
// document.getElementById("loadgif").style.visibility= "hidden";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900