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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:24:45  来源:igfitidea点击:

Redirect to a php page based on submit button clicked

phpjavascripthtmlforms

提问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 atags 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 $_POSTvars along with the redirect, check this other SO post.

要将$_POSTvars 与重定向一起发送,请检查此其他 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 将它们设置为看起来像一个按钮。