javascript 根据单击的提交按钮重定向到 php 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9672959/
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 to a php page based on submit button clicked
提问by abc
I have two submit buttons in a php page. Based on the submit button clicked, I wish to redirect user to different php page. How that can be done?
我在一个 php 页面中有两个提交按钮。基于点击的提交按钮,我希望将用户重定向到不同的 php 页面。那怎么办?
<form>
<input type="submit" value="Go To Page1.php">
<input type="submit" value="Go To Page2.php">
</form>
回答by Basti
Assuming you don't have any shared input boxes, you can just do something like this, or use simple links.
假设你没有任何共享的输入框,你可以做这样的事情,或者使用简单的链接。
<form action="http://example.org/Page1.php">
<input type="submit" value="Go To Page1.php">
</form>
<form action="http://example.org/Page2.php">
<input type="submit" value="Go To Page2.php">
</form>
If you have additional input elements, I suggest looking into this solution. Relevant code sample:
如果您有其他输入元素,我建议您查看此解决方案。相关代码示例:
<input type="submit" name="submit1" value="submit1" onclick="javascript: form.action='test1.php';" />
回答by Jon Egeland
Just use a set of a
tags inside the form
:
只需在 中使用一组a
标签form
:
<form>
<!-- Other Inputs -->
<div id="redirects">
<a href="Page1.php">Go to page 1</a>
<a href="Page2.php">Go to page 2</a>
</div>
</form>
If you need to send certain information along with your redirect, keep your current form and have a condition at the top of your file:
如果您需要随重定向一起发送某些信息,请保留当前表单并在文件顶部设置条件:
<?php
// You will need to send the $_POST data along with the redirect
if(isset($_POST['submit1']))
header('Location: page1.php');
else if(isset($_POST['submit2']))
header('Location: page2.php');
// Continue with the page...
?>
To send the $_POST
vars along with the redirect, check this other SO post.
要将$_POST
vars 与重定向一起发送,请检查此其他 SO post。
回答by Gergo Erdosi
You don't need a form for this, neither input fields. Use <a>
tags instead. You can style them with CSS to look like a button if you want that.
您不需要为此提供表单,也不需要输入字段。改用<a>
标签。如果需要,您可以使用 CSS 将它们设置为看起来像一个按钮。