javascriptphpajax

Display loading circle when button is clicked in PHP website using JavaScript?


I'm new to programming in JavaScript and am having a lot of difficulty doing something I believe is simple. I have a loading circle that I want to display when an upload button is clicked (and also want my external php code to run to do image processing). Then, I want the loading screen to go away once the php page is done loading. I'm currently having trouble even getting the loading screen to show. I have the loading circle code in the style section of my header as so:

<head>
<style>
/* Center the loader */
#loader {
  position: absolute;
  left: 50%;
  top: 25%;
  z-index: 1;
  width: 150px;
  height: 150px;
  margin: -75px 0 0 -75px;
  border: 16px solid #f3f3f3;
  border-radius: 50%;
  border-top: 16px solid #00ff00;
  -webkit-animation: spin 2s linear infinite;
  animation: spin 2s linear infinite;
}

@-webkit-keyframes spin {
  0% { -webkit-transform: rotate(0deg); }
  100% { -webkit-transform: rotate(360deg); }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}


</style>

</head>

Then, I have my upload button and the script in the body as so:

    <p class="text-center"> 
         
        <button  onclick="loadingCircle()"> Click to Upload! </button>
    </p> 
    <div id="loader" style="display:none;"></div> 
<script>
    function loadingCircle() {
        $("#loader").show();
        }
    
    </script>

Currently, when I click the upload button, no action is happening... any help is appreciated and apologies for the noobness.


Solution

  • Add jquery file https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    

    or you can hide by using simple javascript:

    <script>
        function loadingCircle() {
           document.getElementById('loader').style.display='block';
            }
    
        </script>