How to redirect to another page after click on submit button in form with required inputs

Michelle

I need to redirect to index.html after clicking on button in a form. I tried this:

$("#btn").on("click", function() { 
  window.location.replace("index.html");
  localeStorage.clear();
})

However in the form I have required inputs, so when I had some empty required input, it redirected, but at the same time it said that I must write something to input.

I need to redirect to index.html after successful form submission. Like on a eshop after submitting an order

Rory McCrossan

Firstly, if you want the form to be validated first before the redirection you need to place the event on the submit of the form, not the click of the button.

Secondly, performing a redirect in JS when the form is submit is redundant; just set the action of the form to where you want the user to be sent.

With the above in mind, try this:

<form id="yourForm" action="index.html">
  <input type="text" name="foo" required />
  <!-- Some more 'required' form controls... -->

  <button type="submit">Submit</button>
</form>
$("#yourForm").on("submit", function() { 
  localeStorage.clear();
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redirect to another page after form submit

How to redirect another page after button on click method react js

After submit redirect to another page

how add some textbox values in session in jsp page after click a button except form submit?

How do I redirect to another page on form submit?

How to stop redirect from partial page after form submit

On button click- do a redirect and ajax post submit form of new page, with data passed from click

React form not displaying the correct inputs on the screen after I click submit button

submit should not redirect to another page in HTML form

How to click submit button in a form?

How to make non editable form or textfield after click button Submit

How to check that radiobuttons checked on the page after click on submit button?

Redirect to home page after submit form vue

Redirect to anchor in same page after form submit

yet another redirect after form submit

How to redirect from one html page to another on button click in javascript?

How to redirect to another page and specific div on button click in mvc

How to use addEventListener to redirect to another html page on button click?

How to open pop up modal when form submit sucessfully and hide pop after 2 mintues and redirect to another page in react js?

submit form only on button click not on page refresh

How to redirect page after click on Ok button on sweet alert?

Javascript: How to redirect to another page after click play.png?

Getting Page can't be displayed error after Response.Redirect on submit button click

Button click with jquery cancel form and redirect page

How to redirect after click on button

Angular: Click button to redirect to another page not working

How to redirect to another HTML page after submitting a form?

Contact Form - How to not redirect to another page after submission

How to redirect to another page after form submition using a loading screen