2012-03-12 117 views
0

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

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

基本上,没有理由有一个“表单动作”,对吧? – 2012-03-12 18:49:34

+0

我不明白这与PHP有什么关系。 – Basti 2012-03-12 18:56:16

回答

0

你并不需要为这个形式,既不输入字段。改用<a>标签。如果你愿意,你可以用CSS来设计它们的样子。

1

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

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

如果您有其他输入元素,我建议您调查this solution。相关的代码示例:

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

其实我有共享输入框。 – abc 2012-03-12 19:07:59

0

只需使用一组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> 

如果您需要与您的重定向一起发送的某些信息,让您目前的形式,并有一个条件在你的文件的顶部:

<?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瓦尔与重定向一起,检查this other SO post

+1

简单的问题是,如果你只是'header('Location:page1.php');',所有额外的'GET'或'POST'数据将会丢失。这将像点击一个没有通过PHP的HTTP 301重定向的直接链接。 – Basti 2012-03-12 19:05:34

+0

@Basil相应编辑... – Jon 2012-03-12 19:07:02

相关问题