根据单击的提交按钮重定向到php页面

Redirect to a php page based on submit button clicked

本文关键字:重定向 php 页面 按钮 提交 单击      更新时间:2023-09-26

我在php页面中有两个提交按钮。根据点击的提交按钮,我希望将用户重定向到不同的php页面。如何做到这一点?

<form>
<input type="submit" value="Go To Page1.php">
<input type="submit" value="Go To Page2.php">
</form>

假设您没有任何共享的输入框,您可以这样做,或者使用简单的链接。

<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>

如果您有其他输入元素,我建议您研究这个解决方案。相关代码示例:

<input type="submit" name="submit1" value="submit1" onclick="javascript: form.action='test1.php';" />

只需在form:中使用一组a标签

<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>

如果你需要在重定向的同时发送某些信息,请保留你当前的表格,并在文件顶部有一个条件:

<?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...
?>

要在重定向的同时发送$_POST变量,请查看其他SO帖子。

您不需要表单,也不需要输入字段。请改用<a>标记。如果你想要的话,你可以用CSS把它们做成按钮的样式。