0

How can i conditionally set PHP form action?. i have the following drop down option, when 2WD and 4WD selected i want to redirect or go to A.php otherwise the other one MFWD selected to open page B

 <form action="page.php">
    <select name="tractor">
      <option value="2wd">2WD</option>
      <option value="mfwd">MFWD</option>
      <option value="4wd">4WD</option>
    </select>
<input type="submit">

     here is the whole code
    <?php ob_start ?>
<!DOCTYPE html>
 <html>
  <body>

  <form method="post">
    <select name="tractor">
     <option value="2wd"><a href="iar7.php">2WD</a></option>
      <option value="mfwd">MFWD</option>
         <option value="4wd">4WD</option>

   </select>
   <br><br>
  <input type="submit" name="cmd_submit" />

 </form>
<?php
if(isset($_POST['cmd_submit'])){
    if($_POST['tractor'] == "2wd" || $_POST['tractor'] == "4wd"){
         //redirect here
         header('location:iar7.php');
    }else{
         header('location:iar-calculator.php');
     }
   }
  ?>
  </body>
  </html>
Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
Funk
  • 31
  • 8

1 Answers1

0

you can do it like this. omit the action attribute and add a method="post".. otherwise you need to add a get method there... and get the query string of the tractor to do a conditional statement on your action attribute....

HTML Code

<form method="post">
 <select name="tractor">
  <option value="2wd">2WD</option>
  <option value="mfwd">MFWD</option>
  <option value="4wd">4WD</option>
</select>
<input type="submit" name="cmd_submit" />
</form>

PHP Code:

<?php
    if(isset($_POST['cmd_submit'])){
        if($_POST['tractor'] == "2wd" || $_POST['tractor'] == "4wd"){
             //redirect here
             header('location:a.php');
        }else{
             header('location:b.php');
        }
    }
Sam Teng Wong
  • 2,379
  • 5
  • 34
  • 56
  • Sam i get 'Warning: Cannot modify header information - headers already sent by ' – Funk Apr 02 '16 at 02:44
  • @Funk can you add `ob_start` on the first line of your file? or move the php code above your `html` tag.. here read this http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Sam Teng Wong Apr 02 '16 at 02:46
  • I added the whole code i am working on at the bottom of my question – Funk Apr 02 '16 at 02:50
  • @Funk you need to move the php code above html tag i'll edit it for you. – Sam Teng Wong Apr 02 '16 at 02:50