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

Could you provide the parameters for the next() function in Express?

Working with Express.js to build an API has been a game-changer for me. I've learned how to utilize middlewares, handle requests and responses, navigate through different middleware functions... But there's one thing that keeps boggling my mind, ...

What is the reason for my Firestore listener consistently retrieving data from the server despite having offline persistence enabled?

Incorporating Firebase JavaScript Modular Web Version 9 SDK into my Vue 3 / TypeScript application. My understanding is that when utilizing real-time listeners with offline persistence in Firestore, the process should proceed as follows: Upon initializat ...

Encountering an issue upon pressing the 'calculate' button

Currently, I am delving into express node.js and attempting to execute a straightforward calculator code. However, whenever I click on the button, I do not receive any response, and there are no errors in my code either. I find myself a bit lost in figurin ...

Delete unnecessary css code

After years of working on a website, it has grown significantly in size. The CSS files have become quite messy and unclear. I need to figure out how to identify which parts of my extensive CSS file are not being utilized on my homepage so that I can safel ...

Introducing Bootstrap 5 with a sleek two-column layout that effortlessly allows text to wrap around captivating images

Struggling to find a solution for implementing two columns in a row? Look no further! In the left column, insert an image with classes md-3 or lg-4. In the right column, place a lengthy text using classes md-9 or lg-8. If you want the text to wrap around ...

Employing the Jquery AJAX method to retrieve HTML content

We have a detailed guide on extracting PHP values using Jquery AJAX with JSON data type. Below, you will find the necessary code snippets for implementation. HTML CODE <html> <head> <meta http-equiv="Content-Type" content="text/html; c ...

What is the maximum width that can be set for an HTML input control

I'm working on a web page with a row that contains three controls. SomeText Input Button It looks something like this <div> SomeText <Input/> <img> </div> The image is currently floating to the right. ...

Tips for adjusting the width of a Facebook embedded video within its container using ReactPlayer

Has anyone encountered issues with adding an embedded video to a NextJS app using ReactPlayer and Facebook videos? It seems that Facebook does not support the width="100%" attribute, as it always snaps back to 500px. Interestingly, this works wel ...

Integrating redux-form with the react-widgets DateTimePicker component

I am currently working on a project using ReactJS with Redux and I am trying to incorporate a form similar to the one shown in this example: Most of the components are working well, but I am encountering an issue with the DateTimePicker due to the momentL ...

Having a problem with the hover effect on the navbar component

Whenever I hover over the individual links, I notice that the color change doesn't extend all the way up. I have a feeling there is a more efficient way to achieve this than my current approach. Any assistance would be greatly appreciated! HTML: < ...

"Implementing a feature in Angular to display only a single ul element at a time while iterating through a

In the image above, there is an Add Person button. When this button is clicked, a new row labeled Person 1 is created, and this process continues for each subsequent click. At the right end of every row, there is a share icon that, when clicked, should ope ...

Incorporating AJAX into ASP Classic through an include file

Can I change the content of the #result div using ASP Classic's Include.File on click with a Bootstrap nav? HTML <body onload=""> <nav class="navbar navbar-light bg-light sticky-top shadow"> span><%= ...

Identify the class within a child division and include the class in a separate division

How can I detect a special class within a child div and then add a class to another div when that specific class is found? For example: <ul class="m-a-ul"> <li class="menu"> </ul> Now, if the class collapsed is dynamically added: ...

Animate the coloring process with dynamic motion

Is there a way to dynamically fill a canvas with color and apply animation? I have successfully created a canvas cylinder that fills with color, but I'm hoping for a slow animation effect when the color is filled. I've tried searching on Google ...

What could be causing the error message 'Why is 'push' property undefined in React Native?

Within my index.ios.js file, I have the following: renderScene(route, navigator){ return <route.component navigator={navigator} {...route.passProps}/> } as well as render() { return ( <Navigator initialRoute={{name: &a ...

Automatically updating database with Ajax post submission

I have customized the code found at this link (http://www.w3schools.com/PHP/php_ajax_database.asp) to update and display my database when an input box is filled out and a button is clicked to submit. Below is the modified code: <form method="post" acti ...

Creating custom functions within views using Sencha Touch 2

I'm having trouble creating my own function in Sencha Touch 2 and I keep receiving an error: Uncaught ReferenceError: function22 is not defined The issue seems to be coming from my Position.js file located in the View directory. Ext.define(' ...

What is the best way to assign multiple values to certain elements within an array?

I have an array that looks like this: items = { item1: 10, item2: 5, item3: 7, item4: 3, }; I am looking to include additional details in this array, for example: items = { item1: 10 {available: true}, ...

Opposite path matching with Next.js middleware

For my Next.js project, I have implemented a middleware and now I need it to only apply to routes that do not start with /api/. I have checked the documentation but couldn't find a similar example. Is there a way to achieve this without manually excl ...

Venom Bot encounters issue with sending files, displaying "Error processing files" message

When attempting to send a PDF in the code snippet below, I encountered the following error: app.post('/send-pdf',[ body('number').notEmpty(), ], async (req, res) => { const errors = validationResult(req).formatWith(({ msg }) =&g ...