Search code examples
phphtmlformspostphp4

POST to different page depending on SELECTvalue?


I have a form, depending on the selected value of my <select> box I would like to send to a different page.

I do not know the best way to do this, but I am thinking that maybe the best way is to have an intermediate page to always post to, then have the intermediate page check the value sent from the <select> box and forward the POST values to a different page depending on what it is. If this is the best way, how can I do that? If not, what is the best way?

Thanks!!


Solution

  • THat's a good way. The alternative is to use Javascript to intercept the click, rewrite the action property of the form and submit() it.

    For your PHP though:

    First page:

    <form action="intermediary.php" method="post">
      <input id="submitTo">
        <select value="1">goToFirstPage</select>
        <select value="2">goToSecondPage</select>
      </input>
    </form>

    Second page:

    <?php
      $url = "";
      if($_POST["submitTo"] == "1") $url = "http://mysite.com/firstPage.php";

     if($_POST["submitTo"] == "2") $url = "http://mysite.com/secondPage.php";
    
     header ("Location: $url");    
    ?>
    

    Don't put actual URLs in your first page, or you'll run into massive security issues.