abc
abc

Reputation: 1

Redirect to a php page based on submit button clicked

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>

Upvotes: 0

Views: 15241

Answers (3)

Gergo Erdosi
Gergo Erdosi

Reputation: 42093

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.

Upvotes: 0

Jon Egeland
Jon Egeland

Reputation: 12623

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.

Upvotes: 0

Basti
Basti

Reputation: 4042

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';" />

Upvotes: 1

Related Questions