5

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?

Abdan Syakuro
  • 1,034
  • 2
  • 12
  • 26
  • AFAIK once you click the `Submit` button your data gets transferred to your server-side form handling script. So disabling the button can't stop a user from multiple submission. You need to follow some other logic to prevent it if you are looking for preventing duplicate entry. – NewBee May 19 '18 at 04:00
  • do you need to prevent duplicate entries for your database? –  May 19 '18 at 04:04
  • If you need some heavy logic sounding this, that's fine, but there's nothing wrong with disabling the button. You could even replace the text in the button with some loading spinner. – parker_codes May 19 '18 at 04:51
  • See [this](https://stackoverflow.com/questions/49454614/laravel-php-multiple-form-submissions-multiple-click-on-submit-button) – iamab.in May 19 '18 at 08:36
  • 2
    @NewBee how can a casual user do multiple submissions if the submit button is disabled? – Adam Jul 15 '18 at 09:55
  • @AbdanSyakuro did you find any solution of it? – Leena Patel Oct 24 '18 at 10:28

4 Answers4

12

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'];

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Saurabh
  • 2,655
  • 1
  • 20
  • 47
3

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>
  • just like my comment on your same answer on the [other question](https://stackoverflow.com/questions/27682830/prevent-from-submiting-form-several-times-by-clicking-fast-in-laravel), I just want to testify that this solution works on me. if only stackoverflow will allow me to give you 1K credits, I will. `[1+]` – kapitan Aug 09 '23 at 00:22
1

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'); 
});
0

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

Hilmi Hidayat
  • 103
  • 4
  • 16