Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
Hi, Let's see a quick way to prevent your customers or users from submitting forms multiple times with simple CSS & Javascript.
You might have a form for which the HTML might look similar to the following.
<form action="" method="post" class="form-prevent-multiple-submits">
<div class="mt-2">
<textarea name="description" id="description" rows="3" class="form-control form-control-sm"></textarea>
</div>
<div class="mt-1">
<button type="submit" class="btn btn-sm btn-primary button-prevent-multiple-submits">
<i class="fas fa-spinner spinner"></i> Comment
</button>
</div>
</form>
It's just a form with a textarea and submit button which looks like the following. Observe the following classes in form
& submit
the button we will use to disable the submit button.
<form class="form-prevent-multiple-submits">
<button class="button-prevent-multiple-submits">
<i class="fas fa-spinner spinner d-none"></i> Comment
</button>
multiple-form-submit-prevent.css
/** Hide spinner on page load */
.spinner{
display: none;
}
/** Code to rotate the spinnner with keyframes */
.button-prevent-multiple-submits .spinner{
-webkit-animation:spin 4s linear infinite;
-moz-animation:spin 4s linear infinite;
animation:spin 4s linear infinite;
}
@-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
@-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
multiple-form-submit-prevent.js
/** On submit of the form disable the submit button and display the spinner */
$('.form-prevent-multiple-submits').on('submit', function() {
$('.button-prevent-multiple-submits').attr('disabled', true);
$('.button-prevent-multiple-submits .spinner').show();
});
In the following code, I have used FontAwesome, Bootstrap 4, jQuery files. If you have only jQuery then the code is good to go.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Prevent Multiple Form Submit Example</title>
<!-- Fontawesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
<!-- Bootsgrap CSS file -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
<!-- Link to your CSS file -->
<link rel="stylesheet" href="multiple-form-submit-prevent.css">
<!-- Bootstrap & jQuery file -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- Link to your Javascript file -->
<script src="multiple-form-submit-prevent.js"></script>
</head>
<body>
<form action="" method="post" class="form-prevent-multiple-submits">
<div class="mt-2">
<textarea name="description" id="description" rows="3" class="form-control form-control-sm"></textarea>
</div>
<div class="mt-1">
<button type="submit" class="btn btn-sm btn-primary button-prevent-multiple-submits">
<i class="fas fa-spinner spinner d-none"></i> Comment
</button>
</div>
</form>
</body>
</html>
Unable to prepare route [{fallbackPlaceholder}] for serialization. Uses Closure In Laravel
Setup AMP (Accelerated Mobile Pages) In PHP Laravel
Install NGINX In Linux / Ubuntu And Managing
Sass or SCSS @mixin vs @extends vs Placeholder (%)
Localization In Laravel REST API
Integrate Google Translate Into Your Website
Custom Validation Rules In PHP Laravel (Using Artisan Command)
Global Data In All Laravel Blade Pages
Automate Repeating Tasks In Linux Server With Cronjobs
Manipulate HTML Using DOMDocument In PHP
Factories To Speed Up Test-Driven Development In Laravel