1

I have a form with method Post and action with URL, when I click button to submit, after submitting, it gets refreshed. I want to prevent from being refreshed.

I used:

onsubmit=";return false" in form tag but it did not submit the form.

Also tried:

type="button" in save button but it did not submit.

my Form:

<form class="form-horizontal" method="POST" action="{{ url('panel/admin/webscraper') }}" enctype="multipart/form-data">
<input type="hidden" id="csrf_token" name="_token" value="{{ csrf_token() }}">
<input type="text" name="subcategory" value="{{$subcategory}}">
<button type="submit">Save</button>
</form>
Anonymous Girl
  • 582
  • 8
  • 22

3 Answers3

0

Add a submit event to your form and then use preventDefault to prevent refresh, after that do your AJAX

Set id to form

<form id="noRefreshForm" class="form-horizontal" method="POST" action="{{ url('panel/admin/webscraper') }}" enctype="multipart/form-data">

Finally select this form and then use preventdefault

document.getElementById("noRefreshForm").addEventListener("submit", function(event){
// this line prevents refresh the page
  event.preventDefault()

// do your AJAX
});
Ivandez
  • 249
  • 6
  • 13
0

Or just disable button after click:

<form class="form-horizontal" method="POST" action="{{ url('panel/admin/webscraper') }}" enctype="multipart/form-data">
<input type="hidden" id="csrf_token" name="_token" value="{{ csrf_token() }}">
<input type="text" name="subcategory" value="{{$subcategory}}">
<button type="submit" onclick="this.disabled=true">Save</button>
</form>
blacky99
  • 115
  • 1
  • 12
0

Can use method="dialog"

<form onsubmit="postFunction()" method="dialog"></form>

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-action

Devin Peterson
  • 241
  • 1
  • 7