I have use Laravel 9 and in a blade I have form below:
<form id="create-offline-payment-form" action="{{route ('user.cart.pay')}}" method="post">
@csrf
<input type="hidden" value="{{$shop_id}}" id="shop-id" name="shop_id">
<button id="payment" type="submit" class="btn">پرداخت آفلاین</button>
</form>
the form submit to this method:
public function payCart(Request $request)
{
Transaction::create([
'seller_id' => Auth::id(),
'amount' => $this->cartSum(),
]);
return redirect()->route('user.cart.paid');
}
and in the another method by route above, will show a view to user :
route('user.cart.paid') go to method below:
public function paidCart()
{
return view('user.cart.paid');
}
my problem is when a user click several times on submit botton, multiple record gonna be built on Transaction table.
how can I prevent that?
thanks
My Answer:
To prevent multiple form submissions by clicking the submit button multiple times, you can use JavaScript to disable the submit button after it has been clicked once. Here's an example of how you can achieve this in Laravel:
1. Add an `id` attribute to your submit button in your blade file:
html
2. Add the following JavaScript code to your blade file or in a separate JavaScript file:
javascript
document.getElementById('submitBtn').addEventListener('click', function() {
this.disabled = true;
this.form.submit();
});
This code will disable the submit button when it is clicked, preventing multiple form submissions. The form will still be submitted, but the button will be disabled to prevent further submissions.
Make sure to adjust the form action URL (`/submit-form` in this example) to match the route where your form is submitted in your Laravel application.
Rate this post
4 of 5 based on 6485 votesComments