if / elseif `isset` multiple submit inputs doesn't work
Asked Answered
I

1

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?

Inconsequential answered 1/4, 2017 at 11:44 Comment(9)
Check your $_POST in all casesEar
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?Conceive
Dit you give a name to your input button? only the clicked input should be send if u give a name. See answer here #11929971Convolution
Possible duplicate of PHP if-statement ignored when header(Location: xxx) is insideConceive
According to the linked question (possible duplicate), you might need to add exit(); after the calls to header().Conceive
@Ear and @Convolution 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.Inconsequential
btw, always use exit to "commit" your header("location: ...");Sidereal
Can you provide the HTML form too? Maybe a var_dump($_POST) out would be great also.Moujik
It means that noranTelChecks is never empty, please check your html form, I think there is a mistakeHaematoma
M
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' );
}
Mahatma answered 27/4, 2017 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.