在 HTML 中提交表单后重定向到页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44221250/
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
Redirecting to a page after submitting form in HTML
提问by Ons Ali
I'm fairly new to coding in HTML. After hours of searching the internet for a way to do this, I failed and so I'm here. I was setting up a CSRF Proof of concept page here, I want it to redirect to another page which will execute the payload that the CSRF had implemented.
我对 HTML 编码相当陌生。在互联网上搜索了数小时的方法后,我失败了,所以我在这里。我在这里设置了一个 CSRF 概念证明页面,我希望它重定向到另一个页面,该页面将执行 CSRF 已实现的有效负载。
<html>
<body>
<form action="https://website.com/action.php?" method="POST">
<input type="hidden" name="fullname" value="john" />
<input type="hidden" name="address" value="street 2, 32 ave" />
<input type="submit" value="Submit request" />
</form>
</body>
</html>
So after this form is submitted using, all it does is redirect to this page
所以在使用提交这个表单之后,它所做的就是重定向到这个页面
But instead of that, I want it to redirect to another URL as well as submit that form.
但不是这样,我希望它重定向到另一个 URL 并提交该表单。
回答by Ons Ali
For anyone else having the same problem, I figured it out myself.
对于其他有同样问题的人,我自己解决了。
<html>
<body>
<form target="_blank" action="https://website.com/action.php" method="POST">
<input type="hidden" name="fullname" value="Sam" />
<input type="hidden" name="city" value="Dubai " />
<input onclick="window.location.href = 'https://website.com/my-account';" type="submit" value="Submit request" />
</form>
</body>
</html>
All I had to do was add the target="_blank" attribute to inline on form to open the response in a new page and redirect the other page using onclick on the submit button.
我所要做的就是将 target="_blank" 属性添加到表单上的内联,以在新页面中打开响应并使用提交按钮上的 onclick 重定向其他页面。
回答by Hardik Sanghavi
You need to use the jQuery AJAX or XMLHttpRequest() for post the data to the server. After data posting you can redirect your page to another page by window.location.href
.
您需要使用 jQuery AJAX 或 XMLHttpRequest() 将数据发布到服务器。发布数据后,您可以通过 将您的页面重定向到另一个页面window.location.href
。
Example:
例子:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
window.location.href = 'https://website.com/my-account';
}
};
xhttp.open("POST", "demo_post.asp", true);
xhttp.send();