Joren Willems
Joren Willems

Reputation: 167

Disallow multiple submits

On my form I have 1 submit form which inserts data into my DB. Once the user clicks on the submit button the page is taking too long to redirect to the next page wherefore the user can click it a second and maybe a third time. The insert is in that case done more than 1 time.

Is it possible to disable the submit button after it is clicked the first time? Or do you have another sollution for my problem.

Upvotes: 0

Views: 51

Answers (3)

PHP Geek
PHP Geek

Reputation: 4033

If you are using AJAX then disable the button till response returned as follows:

$("#btn").attr('disabled','true');

else you can disable button on click of button and after submission successful reload the page again. It will resolve your multiple submission of the form.

$("#btn").on('click',function(){
 $("#btn").attr('disabled','true');
});

and after inserting the data into the database user header to redirect on the same url.

Upvotes: 0

Stan
Stan

Reputation: 26501

If you can use javascript you can just disable button after you click it like so.

$('.submit-button').on('click', function (e) {
	e.preventDefault();
	$(this).attr('disabled', 'disabled');
  $(this).closest('form').submit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
  <input class="submit-button" type="submit">
</form>

Upvotes: 2

Minstel
Minstel

Reputation: 626

It is possible. Just add attr disabled to your button after user clicks it.

With jQuery it can be done like this:

$(document).on('submit', '.my-submit-button', function(e) {
    e.preventDefault();
    $(this).attr('disabled', 'disabled');
});

Upvotes: 0

Related Questions