How do I redirect users after submit button click?

2020-02-05 07:02发布

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>

标签: javascript
7条回答
Deceive 欺骗
2楼-- · 2020-02-05 07:43
// 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";
查看更多
▲ chillily
3楼-- · 2020-02-05 07:44

It would be

window.location="login.php";
查看更多
家丑人穷心不美
4楼-- · 2020-02-05 07:45

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>

查看更多
Explosion°爆炸
5楼-- · 2020-02-05 07:46

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>

查看更多
太酷不给撩
6楼-- · 2020-02-05 07:47

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");
查看更多
疯言疯语
7楼-- · 2020-02-05 07:50

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.

查看更多
登录 后发表回答