2

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>
7
  • 1
    In that case you should use AJAX requests Commented Jul 23, 2021 at 17:51
  • well if you want to submit the form with normal HTML, the page is going to refresh. Commented Jul 23, 2021 at 17:54
  • 2
    @epascarello using Laravel, any JS or Php method to try? Commented Jul 23, 2021 at 17:55
  • 1
    webtrickshome.com/forum/… Commented Jul 23, 2021 at 18:22
  • 2
    Ajax would be the best solution for this. Commented Jul 28, 2021 at 11:04

3 Answers 3

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
});
Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

0

Can use method="dialog"

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

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

3 Comments

This does not submit the form, but triggers an event instead
that depends on postFunction(ajax)
Does it depend on the return value of the handler?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.