7

I have several submit inputs (buttons) on a page, with the following issetconditions:

if ( isset( $_POST[ 'noranTelChecks' ] ) ) { // user requested noranTelCheck sheet
    header( 'location: noranTelChecks.php' );

  } elseif ( isset( $_POST[ 'juniperChecks' ] ) ) { // user requested noranTelCheck sheet
    header( 'location: juniperChecks.php' );

  } elseif ( isset( $_POST[ 'mpr95001Checks' ] ) ) { // user requested noranTelCheck sheet
    header( 'location: mpr95001Checks.php' );
} // close IF

But no matter what button is clicked the page is always redirected to the link referred to by the first IF condition. If I change the order of the links referred to, it is always the link in the first condition that the page gets redirected to.

What could be the problem with the above code causing this issue, as I have done this in the past on other pages and it has worked fine?

commnux
  • 485
  • 2
  • 9
  • 1
    Check your `$_POST` in all cases – u_mulder Apr 01 '17 at 11:49
  • While I don't know what the cause is, you could try to use a variable for the location target (`$location`) within your if/elseif, then just make one single call to `header('location: ' . $location);` outside of it - maybe that helps somehow? – domsson Apr 01 '17 at 12:01
  • 1
    Dit you give a name to your input button? only the clicked input should be send if u give a name. See answer here http://stackoverflow.com/questions/11929471/how-do-i-use-two-submit-buttons-and-differentiate-between-which-one-was-used-to – Incognito Apr 01 '17 at 12:02
  • Possible duplicate of [PHP if-statement ignored when header(Location: xxx) is inside](http://stackoverflow.com/questions/6149941/php-if-statement-ignored-when-headerlocation-xxx-is-inside) – domsson Apr 01 '17 at 12:02
  • According to the linked question (possible duplicate), you might need to add `exit();` after the calls to `header()`. – domsson Apr 01 '17 at 12:03
  • 1
    @u_mulder and @Incognito thanks for pointing me in the direction of the problem. It turns out the first submit button had the same name as a radio button that had its `value=1` and so I guess the `isset` condition was always being met no matter what button is clicked. – commnux Apr 01 '17 at 12:13
  • btw, always use exit to "commit" your header("location: ..."); – Fabien Haddadi Apr 18 '17 at 07:56
  • Can you provide the HTML form too? Maybe a var_dump($_POST) out would be great also. – Peter Apr 18 '17 at 12:53
  • It means that `noranTelChecks` is never empty, please check your html form, I think there is a mistake – Arkadi Apr 27 '17 at 17:14

1 Answers1

1

I guess your buttons have values set for example

<input type="submit" id="noranTelChecks" name="noranTelChecks" value="Button 1"/>

so you could use the value instead of name or id. The code would be as follow

if ( isset( $_POST["Button 1"] ) ) 
{ 
    header( 'location: noranTelChecks.php' );
}