0

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?

<form>
<input type="submit" value="Go To Page1.php">
<input type="submit" value="Go To Page2.php">
</form>
Basti
  • 3,998
  • 1
  • 18
  • 21
abc
  • 1
  • 1
  • 2
  • 3

3 Answers3

1

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';" />
Basti
  • 3,998
  • 1
  • 18
  • 21
0

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.

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
0

Just use a set of a tags inside the 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.

Community
  • 1
  • 1
Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
  • 1
    The simple problem is that all the additional `GET` or `POST` data will be lost if you just `header('Location: page1.php');`. This will be like clicking a direct link without the HTTP 301 redirect through PHP. – Basti Mar 12 '12 at 19:05