Redirection after successful form submit
You should bind to the submit
event, not click
event:
UPDATED TO MATCH THE COMMENTS
$(function () {
var submityesClicked;
//catch the click to buttons
$('#submityes').click(function () {
submityesClicked = true;
});
$('#submitno').click(function () {
submityesClicked = false;
});
$('#webform').submit(function (e) {
e.preventDefault();//prevent the default action
$.ajax({
type: "POST",
/*url: "process.php", //process to mail
data: $('form.contact').serialize(),*/
success: function (msg) {
window.location.replace(submityesClicked ? "/submit_resolved_yes.php" : "/submit_resolved_no.php");
},
error: function () {
alert("error");
}
});
});
});
The submit
event is triggered only if the form is valid.
Note that the submit
event is triggered by the form but the click
event is triggered by the input
element.
Redirect after submitting a form (HTML, PHP)
First, remove the inline handler on your submit button. Your HTML should look like so:
<form method="post" action="" id="formwrap">
<!-- questions here -->
<input id="submitButton" class="block" type="submit" name="verzenden" value="Opslaan" />
</form>
Then, update your JavaScript to add an event handler that targets your formwrap
element's submit
event. Then prevent the form from making a post-back with preventDefault
. Once the default behavior is prevented, you can send the user to whatever url you want :)
document
.getElementById("formwrap")
.addEventListener("submit", function(e) {
e.preventDefault();
window.location.href = "test.php";
});
How to redirect to another page after click on submit button in form with required inputs
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();
});
Redirect another page after successful form submission
Replace header("Location:http://www.google.com");
with:
window.location.href = 'https://www.google.com';
header()
is a PHP function which means "before you send the response back to the client (browser), add the Location:
header. This header tells chrome to redirect using a 301 or 302 status code. PHP code obviously won't work in Javascript.
Considering javascript ONLY happens in the browser there are no headers that need to be set. You can just tell javascript to directly navigate the current window to the URL you specify.
Related Topics
How to Prevent Text from Overflowing in CSS
Why Use Definition Lists (Dl,Dd,Dt) Tags for HTML Forms Instead of Tables
CSS - Hover Passes Through Elements to Activate Hover on Covered Element
Stop Lastpass Filling Out a Form
How to Detect HTML5 Audio Mp3 Support
How to Create Curved & Overlapping Menu Tabs in CSS
Assigning Multiple Styles on an HTML Element
Prevent a Flex Items Height from Expanding to Match Other Flex Items
Make a Perfect Circle Around a Div of Variable Height
Can a Div Have Multiple Classes (Twitter Bootstrap)
How to Stretch Div Height to Fill Parent Div - CSS
Is It Ok to Use <Strong> in Place of <B> Blindly
Bootstrap 5 Layout for Different Sizes Cards - Like Pinterest