javascript - Redirect to a php page based on submit button clicked - Stack Overflow

I have two submit buttons in a php page. Based on the submit button clicked, I wish to redirect user to

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>

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>
Share Improve this question edited Mar 12, 2012 at 18:56 Basti 4,0411 gold badge21 silver badges22 bronze badges asked Mar 12, 2012 at 18:46 abcabc 11 gold badge2 silver badges3 bronze badges 2
  • So basicly , there's no reason to have a form action , right? – Ofir Baruch Commented Mar 12, 2012 at 18:49
  • I don't see how this has anything to do with PHP. – Basti Commented Mar 12, 2012 at 18:56
Add a ment  | 

3 Answers 3

Reset to default 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/Page1.php">
    <input type="submit" value="Go To Page1.php">
</form>

<form action="http://example/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';" />

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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248851a4618553.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信