How do I redirect users after submit button click? My javascript isn\'t working:
Javascript
Why don't you use plain html?
<form action="login.php" method="post" name="form1" id="form1">
...
</form>
In your login.php you can then use the header() function.
header("Location: welcome.php");
use
window.location.replace("login.php");
or simply window.location("login.php");
It is better than using window.location.href =,
because replace()
does not put the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href
. If you want to simulate an HTTP redirect, use location.replace
.
Using jquery you can do it this way
$("#order").click(function(e){
e.preventDefault();
window.location="login.php";
});
Also in HMTL you can do it this way
<form name="frm" action="login.php" method="POST">
...
</form>
Hope this helps
It would be
window.location="login.php";
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com/SpecificAction.php");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com/SpecificAction.php";
Your submission will cancel the redirect or vice versa.
I do not see the reason for the redirect in the first place since why do you have an order form that does nothing.
That said, here is how to do it. Firstly NEVER put code on the submit button but do it in the onsubmit, secondly return false to stop the submission
NOTE This code will IGNORE the action and ONLY execute the script due to the return false/preventDefault
function redirect() {
window.location.replace("login.php");
return false;
}
using
<form name="form1" id="form1" method="post" onsubmit="return redirect()">
<input type="submit" class="button4" name="order" id="order" value="Place Order" >
</form>
Or unobtrusively:
window.onload=function() {
document.getElementById("form1").onsubmit=function() {
window.location.replace("login.php");
return false;
}
}
using
<form id="form1" method="post">
<input type="submit" class="button4" value="Place Order" >
</form>
jQuery:
$("#form1").on("submit",function(e) {
e.preventDefault(); // cancel submission
window.location.replace("login.php");
});
Example:
$("#form1").on("submit", function(e) {
e.preventDefault(); // cancel submission
alert("this could redirect to login.php");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<form id="form1" method="post" action="javascript:alert('Action!!!')">
<input type="submit" class="button4" value="Place Order">
</form>