javascript 发布 HTML 表单后重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12092226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect after posting a HTML form
提问by Chris Headleand
Is there anyway to redirect the user after posting a HTML form?
无论如何在发布 HTML 表单后重定向用户?
I have a form which looks like this
我有一个看起来像这样的表格
<form action="/URL/" method="post">
<input type="radio" name="answer" value="W" />W<br />
<input type="radio" name="answer" value="X" />X<br />
<input type="radio" name="answer" value="Y" />Y<br />
<input type="radio" name="answer" value="Z" />Z<br />
<input type="submit" />
</form>
Is there anyway I can redirect the user to a new page after they click the submit button? I found a bit of javascript however that seemed to stop the post from working :S
无论如何,我可以在用户单击提交按钮后将用户重定向到新页面吗?我发现了一些 javascript 但是这似乎阻止了帖子的工作:S
Cheers!
干杯!
回答by nickb
If you're using PHP, do it on the server:
如果您使用的是 PHP,请在服务器上执行:
if( isset( $_POST['answer'])) {
header( 'Location: http://www.example.com/newurl');
exit();
}
Note that you should be using a full URL in the header()
call, and that you likely want to exit()
after calling header()
in this case.
请注意,您应该在header()
调用中使用完整的 URL ,并且在这种情况下您可能希望exit()
在调用后使用header()
。
回答by Fluffeh
You can use a simple php header('yourlocation.php');
to move them away from the form.
您可以使用简单的 phpheader('yourlocation.php');
将它们从表单中移开。
The PHP that your form action pointed to will still continue executing even if your header() is at the top of the php file.
即使您的 header() 位于 php 文件的顶部,您的表单操作指向的 PHP 仍将继续执行。
The header redirect will take care of the user hitting refresh - it has already moved them past the point of sending form data, so even if they refresh, it will just open the page again sans any form submissions.
标题重定向将处理用户点击刷新 - 它已经将它们移到发送表单数据的位置,因此即使它们刷新,它也只会再次打开页面而无需提交任何表单。
An example is as fololows:
一个例子如下:
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
// Process Form Data, insert to database, whatever you want... then
header('Location: http://www.example.com/');
?>
The user then ends up at wwww.example.com and they can spam refresh as much as they like, but all the processing has already taken place. It won't resend the information (or process their payment again or anything else).
然后用户最终访问 wwww.example.com,他们可以随意刷新垃圾邮件,但所有处理都已经发生。它不会重新发送信息(或再次处理他们的付款或其他任何事情)。
回答by Madara's Ghost
Yes, use a redirect header:
是的,使用重定向标头:
header("Location: http://new.location.com/w00t");
This must be called before any output is sent.
这必须在发送任何输出之前调用。
Take note that everything afterthe header call would still be executed! So you'll need die()
or exit()
afterwards to prevent unexpected results.
请注意,header 调用之后的所有内容仍然会被执行!因此,您需要die()
或exit()
之后才能防止出现意外结果。
回答by dpitkevics
You can just simply specify URL to which form will go after being submitted.
您可以简单地指定表单提交后将转到的 URL。
<form action="http://localhost/redirected.php" method="post">
You can check with PHP whether form is submitted and then redirect:
您可以使用 PHP 检查表单是否已提交,然后重定向:
if (isset($_POST['answer']))
header('Location: redirect.php');
And You can achieve this with javascript/jquery.
您可以使用 javascript/jquery 来实现这一点。
$('#submit_button').click(function () {
// do everything with variables
window.location = 'redirect.php';
});
回答by WTK
Yep, just post the data using ajax and then use window.location = 'http://xxxx.com'
to redirect user to location of your choice.
That's assuming you want to redirect user without using server-side location headers (like the other ones posted).
是的,只需使用 ajax 发布数据,然后用于window.location = 'http://xxxx.com'
将用户重定向到您选择的位置。那是假设您想在不使用服务器端位置标头的情况下重定向用户(如发布的其他位置标头)。