Pressing the reset button will initiate once the loader has finished

Hello everyone, I'm currently working on creating a button that transforms into a loader when submitted and then reverts back to a button after the form is successfully submitted.

I suspect the issue lies within my JavaScript. I'm not sure how to implement a timeout in my JS code to hide the loader after a certain period or after the PHP script has processed the submission. My form uses phpmailer and through ajax displays an alert message indicating that the form has been sent, but sometimes there's a delay in showing the message, leading clients to submit the form multiple times

function ButtonClicked() {
  document.getElementById("formsubmitbutton").style.display = "none"; // hide the button
  document.getElementById("buttonreplacement").style.display = ""; // display the loader
  return true;
}
var FirstLoading = true;

function RestoreSubmitButton() {
  if (FirstLoading) {
    FirstLoading = false;
    return;
  }
  document.getElementById("formsubmitbutton").style.display = ""; // display the button
  document.getElementById("buttonreplacement").style.display = "none"; // hide the loader
}
// To disable restoring submit button, disable or delete next line.
document.onfocus = RestoreSubmitButton;
.cssload-container {
  position: relative;
  height: 40px;
  width: 40px;
  margin: auto;
  margin-top: 2%;
}

.cssload-whirlpool,
.cssload-whirlpool::before,
.cssload-whirlpool::after {
  position: absolute;
  top: 50%;
  left: 50%;
  border: 1px solid #08d108;
  border-left-color: rgb(0, 0, 0);
  border-radius: 924px;
  -o-border-radius: 924px;
  -ms-border-radius: 924px;
  -webkit-border-radius: 924px;
  -moz-border-radius: 924px;
}

.cssload-whirlpool {
  margin: -23px 0 0 -23px;
  height: 40px;
  width: 40px;
  animation: cssload-rotate 1450ms linear infinite;
  -o-animation: cssload-rotate 1450ms linear infinite;
  -ms-animation: cssload-rotate 1450ms linear infinite;
  -webkit-animation: cssload-rotate 1450ms linear infinite;
  -moz-animation: cssload-rotate 1450ms linear infinite;
}

.cssload-whirlpool::before {
  content: "";
  margin: -21px 0 0 -21px;
  height: 41px;
  width: 41px;
  animation: cssload-rotate 1450ms linear infinite;
  -o-animation: cssload-rotate 1450ms linear infinite;
  -ms-animation: cssload-rotate 1450ms linear infinite;
  -webkit-animation: cssload-rotate 1450ms linear infinite;
  -moz-animation: cssload-rotate 1450ms linear infinite;
}

.cssload-whirlpool::after {
  content: "";
  margin: -27px 0 0 -27px;
  height: 52px;
  width: 52px;
  animation: cssload-rotate 2900ms linear infinite;
  -o-animation: cssload-rotate 2900ms linear infinite;
  -ms-animation: cssload-rotate 2900ms linear infinite;
  -webkit-animation: cssload-rotate 2900ms linear infinite;
  -moz-animation: cssload-rotate 2900ms linear infinite;
}

@keyframes cssload-rotate {
  100% {
    transform: rotate(360deg);
  }
}

@-o-keyframes cssload-rotate {
  100% {
    -o-transform: rotate(360deg);
  }
}

@-moz-keyframes cssload-rotate {
  100% {
    -moz-transform: rotate(360deg);
  }
}
<div class="col-md-4 col-md-offset-4">
  <div id="formsubmitbutton">
    <button onclick="ButtonClicked()" name="submit" type="submit" class="btn btn-primary btn-block btn-lg">Send <i class="ion-android-arrow-forward"></i></button>
  </div>
  <div id="buttonreplacement" style="display:none;">
    <div class="cssload-container">
      <div class="cssload-whirlpool"></div>
    </div>
  </div>
</div>

Answer №1

To ensure smooth functionality, it is recommended to invoke ButtonClicked and RestoreSubmitButton within the ajax request as shown below:

function sendFormData(){
    ButtonClicked(); //Executing loading indicator display before sending the request

   $.ajax({
         url:'ServiceToFetchDocType',
         type:'post',
         cache:false,
         success: function(response){
             /* The Ajax request has been completed, allowing you to restore the button functionality */
             RestoreSubmitButton();
        }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps to forward a restricted user to a specific webpage

I am currently utilizing NextJs and am in the process of creating a redirecting function for users who have been banned or blocked from accessing the DB/session. My attempt at this involved: redirect.js, where I created a custom redirect function. impo ...

When you tap on the child element, ignore the parent element

Is there a way to make CSS understand that clicking on a child element within the parent should not be considered as clicking on the parent as well? Why not give it a try by clicking on one of the children inside the parent? .parent{ background: b ...

What is the best way to access the rendered child components within a parent component?

I am seeking a way to retrieve only the visible child components within a parent component. Below is my unsuccessful pseudo-code attempt: parent.component.html <parent (click)="changeVisibility()"> <child *ngIf="visible1"></child> ...

Please provide both an image and text in addition to a progress bar by using the form to

I am working on a form that needs to store both images and text data in a database. <form action="processupload.php" method="post" enctype="multipart/form-data" id="UploadForm"> <input name="name" type="text" /> <input name="age" ty ...

Extracting web search result URLs using Puppeteer

I'm currently facing an issue with the code I've written for web scraping Google. Despite passing in a specific request, it is not returning the list of links as expected. I am unsure about what might be causing this problem. Could someone kindly ...

How to create a vertically scrollable div within another div by utilizing scrollTop in jQuery

I need assistance with scrolling the #container div inside the .bloc_1_medias div. The height of #container is greater than that of .bloc_1_medias. My goal is to implement a function where clicking on the "next" and "previous" text will scroll the #contai ...

Live Update Google Sheet Data to JSON Without Reloading Web Page

This particular function is executing smoothly. My main concern lies in updating a DOM element without reloading the webpage, if any alterations are made to the data on a Google sheet I am utilizing a JSON file from Google sheets: https://spreadsheets.g ...

Unable to apply inset box-shadow to image

I've added a box-shadow to my image. box-shadow: 0 0 10px RED inset However, the shadow is not showing up on the image. When I use Firebug to change the image path, I can see that the shadow is behind the image. How can I apply the shadow directly ...

How can I refresh the container with a new version of the file using PDFObject?

I'm having trouble with a button that generates a PDF and then reloads the PDF in the browser by replacing the container it's in, but with a different link. Despite my efforts, the new PDF does not show up and the old one remains even after refre ...

"Image Reactor: Unleashing the Power

I'm struggling to understand why my relative path image loading isn't functioning correctly. The assets are located within the src folder in my working directory, but for some reason, they're not displaying as expected. When I directly impo ...

Instantly magnifying on the initial point regardless of the zoom type chosen is a feature of Chart.js zoom

Despite adding zoom functionality to my line chart, I am facing an issue where it automatically zooms in to the first point and does not allow me to zoom back out, except for using the reset zoom function. The zoom out function is also not working properly ...

Is it possible to capture and store server responses on the client side using Node.js and React Native?

Here's a POST request I have: router.post("/projects", async (req, res) => { const { projectName, projectDescription, projectBudget, projectDuration, industry, companyName, numberOfEmployees, diamond, } = req.bod ...

What causes the tweets' IDs to be altered by axios when parsing the response from the Twitter search API?

I am currently utilizing axios to send a GET request to the Twitter search API in order to fetch recent tweets that utilize a specific hashtag. To begin with, I ran tests on the twitter search API via Postman and noticed that the id and id_str tweet statu ...

What causes the cancel button to add spaces and create a line break in the textarea field?

Whenever I click the "cancel" button, the textarea inexplicably receives 26 spaces before any existing text and a carriage return after it. This issue occurs when the cancel button is styled in the traditional HTML manner: <button type="reset" name="re ...

Troubleshooting issue with CSS code for table scrolling functionality

I have created an HTML page with a table that gets data from a database. I am trying to limit the size of the table by placing it inside a div like this: <div id="scrollablebody"> <table class="clientTable"> <thead> <tr ...

Exploring the features of mobile search criteria filtration in jQuery

I have a homepage with various criteria that users can select such as budget maximum and minimum. When they click on the "search" button, I want it to lead them to a list of links on another page that corresponds to their search using jQuery Mobile and ...

Is there a way to adjust the position of the scrolling bar to be closer to the post area?

I was exploring this page (http://noahsdad.com/state-fair-texas-2011/) and noticed a scrolling sidebar to the left of the content area. I'm looking to adjust it so that it is closer to the content area. Any suggestions on how to achieve this? Thank y ...

How can I show the configuration in Laravel?

After updating my pages to utilize an extended header for consistent content across all pages, I encountered an issue with the footer configuration. Whenever I attempt to retrieve a configuration from Laravel, it appears as normal text instead of being pro ...

Guide on transferring numerous files (50k+) from a folder to AWS S3 using node.js

Currently, my Node.js API on a Windows machine is generating numerous XML files that are later sent to an S3 bucket. The quantity of files sometimes surpasses 50k. For the uploading process, I am utilizing the aws-sdk package. Essentially, I iterate throu ...

The white-spaces in Quill JS do not retain their original formatting

I recently integrated Quill JS editor into my website. During testing, I noticed that any text inputted after a white space gets emitted when alerting the content. Below is the HTML code snippet: <div id="postCommentEditor" class="postCo ...