14

How do I redirect users after submit button click? My javascript isn't 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>
Joe
  • 15,205
  • 8
  • 49
  • 56
  • Duplicate of [this question](http://stackoverflow.com/questions/15196789/in-php-javascript-window-location-href-not-working-on-submit-button) – Voitcus Mar 05 '13 at 09:39

7 Answers7

20

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>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • I tried it and I always get redirected to the startpage after login, whatever I enter... – Black Sep 12 '18 at 10:11
  • Well, the forms from your examples have no action attribut. Try your solution again with an action which sends the request to a server script then you will understand. – Black Sep 12 '18 at 10:56
  • That is the whole point of the code. It ignores the action and does whatever is in the script. – mplungjan Sep 12 '18 at 11:01
  • If you want to redirect to somewhere after the action, have the server redirect in the action code and ONLY have an action, no script needed – mplungjan Sep 12 '18 at 11:03
  • Nope. It gets ignored though, the redirect from the action script is applied and the one with window.location.replace is ignored. – Black Sep 12 '18 at 11:04
  • Only if there is an error in the script (paste error or missing quotes). Look in the console. What "action script" ? – mplungjan Sep 12 '18 at 11:10
4

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

Joe
  • 15,205
  • 8
  • 49
  • 56
Roger
  • 1,693
  • 1
  • 18
  • 34
3

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.

masterofdestiny
  • 2,751
  • 11
  • 28
  • 40
  • nothing i stand up on the same page after click submit button –  Mar 05 '13 at 09:44
  • go in your firebug click on consol and then click on place order button some error you will get in your firebug .Paste that error here – masterofdestiny Mar 05 '13 at 09:46
2

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");
Joe
  • 15,205
  • 8
  • 49
  • 56
Chris
  • 4,255
  • 7
  • 42
  • 83
  • chris i have tried but i have shopping cart if i use action then qty*amt will not work! –  Mar 05 '13 at 09:40
  • You should have wrote that in your question. `
    ` is the easiest way and will also work without javascript.
    – Chris Mar 05 '13 at 09:42
0

It would be

window.location="login.php";
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
0

I hope this might be helpful

<script type="text/javascript">
 function redirect() {
  document.getElementById("formid").submit();
 }
 window.onload = redirect;
</script>
<form id="formid" method="post" action="anypage.jsp">
  .........
</form>
vvvvvvvvvv
  • 55
  • 6
-1
// 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";
Rubyist
  • 6,486
  • 10
  • 51
  • 86