Abdan Syakuro
Abdan Syakuro

Reputation: 1054

Prevent Multiple Submitting in one button laravel

Before i make this question i use javascript method to prevent multiple submit on my blade template. But i know it's client side that still possible to get attack by.

This is my javascript code

<script>
    function submitForm(btn) {
        // disable the button
        btn.disabled = true;
        // submit the form    
        btn.form.submit();
    }
</script>

<input id="submitButton" type="button" value="Submit" onclick="submitForm(this);" />

my question is, is there another way to prevent without client side in laravel?

Upvotes: 5

Views: 21086

Answers (4)

Hilmi Hidayat
Hilmi Hidayat

Reputation: 425

Step 1: give id to form

<form action="{{ route('web.reports.store') }}" method="POST" enctype="multipart/form-data" id="kt_stepper_form">

Step 2: give id or add class to submit button

<button type="submit" class="btn btn-primary submit-btn" data-kt-stepper-action="submit">
    <span class="indicator-label">
        Submit
    </span>
    <span class="indicator-progress">
        Please wait... <span
            class="spinner-border spinner-border-sm align-middle ms-2"></span>
    </span>
</button>

Step 3: and then, you can add some jquery script like this

$('#kt_stepper_form').on('submit', function(){
    $('.submit-btn').attr('disabled', true);
    $('.indicator-label').hide();
    $('.indicator-progress').show();
});

with code above, button will be disabled and show indicator progress when user clicked the button

Upvotes: 0

Sree Saradh
Sree Saradh

Reputation: 11

give id to submit button

<input class="main-btn" id="register" type="submit" value="Make Appointment">

give id to form

 <form id="appointment_form" method="post" action="{{route('appointment')}}">

in your js add these

$('#appointment_form').on('submit', function () {
   $('#register').attr('disabled', 'true'); 
});

Upvotes: 1

Saurabh
Saurabh

Reputation: 2775

The most straightforward way to guarantee the uniqueness of a form submission (In the sense of stopping someone mashing submit twice) is to generate a random token and storing it in a session AND a hidden field.

If it doesn't match, reject the form, if it does match, accept the form and nuke the session key.

OR

Force Laravel to regenerate a new session token after each time a token is verified correctly. (Easy Way Out)

To achieve this, create a new function tokensMatch() in app/Http/Middleware/VerfiyCsrfToken.php (which will overwrite the inherited one). Something like this:

protected function tokensMatch($request)
{
    $tokensMatch = parent::tokensMatch($request);

    if ($tokensMatch) {
        $request->session()->regenerateToken();
    }

    return $tokensMatch;
}

In case you validate the form and the validation fails, the old data will be passed back to the form. So you need to make sure not to pass back the old token by adding _token to the $dontFlash array in app/Exceptions/Handler.php

protected $dontFlash = ['password', 'password_confirmation', '_token'];

Upvotes: 12

Tanvir Rahman Prince
Tanvir Rahman Prince

Reputation: 41

Step 1: write a class name in the form tag Exp: "from-prevent-multiple-submits"

<form class="pt-4 from-prevent-multiple-submits" action="{{ route('messages.store') }}" method="POST">
            @csrf

Step 2: Write a class in button section

 <button type="submit" id="submit" class="btn btn-primary from-prevent-multiple-submits">{{ translate('Send') }}</button>

Step 3: write this script code

<script type="text/javascript">
(function(){
$('.from-prevent-multiple-submits').on('submit', function(){
    $('.from-prevent-multiple-submits').attr('disabled','true');
})
})();
</script>

Upvotes: 3

Related Questions