How to redirect users after clicking the submit button?

How to redirect users after clicking the submit button? My javascript is not working:

Javascript

<script type="text/javascript" language="javascript"> function redirect() { window.location.href="login.php"; } </script> 

Form page

 <form name="form1" id="form1" method="post"> <input type="submit" class="button4" name="order" id="order" value="Place Order" onclick="redirect();" > </form> 
+7
source share
7 answers

Your message cancels the redirect or vice versa.

I see no reason to redirect in the first place, since you have an order form that does nothing.

So here is how to do it. Firstly, NEVER add code to the submit button, but do it onsubmit, and secondly, return false to stop sending

 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"); }); 
+12
source

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

+3
source

use

 window.location.replace("login.php"); 

or simply window.location("login.php");

This is better than using window.location.href =, because replace() does not put the original page in the session history, that is, the user does not focus on an endless debacle. If you want to mimic someone by clicking the link, use location.href . If you want to simulate HTTP redirection, use location.replace .

+1
source

Why aren't you using plain html?

 <form action="login.php" method="post" name="form1" id="form1"> ... </form> 

In your login.php you can use the header () function.

 header("Location: welcome.php"); 
+1
source

This will

 window.location="login.php"; 
0
source
 // 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"; 
0
source

I hope this can be useful

 <script type="text/javascript"> function redirect() { document.getElementById("formid").submit(); } window.onload = redirect; </script> 
 <form id="formid" method="post" action="anypage.jsp"> ......... </form> 
0
source

All Articles