user3322610
user3322610

Reputation: 39

php if else issue, always displays the else result

$sublevel = $_POST['level'];
echo $sublevel;
if($sublevel == "English"){
    echo "En";
}elseif($sublevel == "Computer"){
    echo "Com";
}else{
    echo "Error";
}

Why won't my if else display En or Com results? Instead it will only display "Error" even if I enter the correct value -- "English" or "Computer". I also checked the value I entered with the echo $sublevel.

Upvotes: 0

Views: 64

Answers (2)

John Conde
John Conde

Reputation: 219864

You named tour variable $sublevel but check $ordersublevel in your if/else statements

$sublevel = $_POST['level'];
if($sublevel == "English"){
    echo "En";
}elseif($sublevel == "Computer"){
    echo "Com";
}else{
    echo "Error";
}

or

$ordersublevel = $_POST['level'];
if($ordersublevel == "English"){
    echo "En";
}elseif($ordersublevel == "Computer"){
    echo "Com";
}else{
    echo "Error";
}

update

Your code should work as is. The only thing I can think of that might cause the problem that you may be overlooking is leading or trailing spaces on the submitted data. Use trim() to remove that.

$sublevel = trim($_POST['level']);

Upvotes: 2

h2ooooooo
h2ooooooo

Reputation: 39540

Because $ordersublevel never gets set. Only $sublevel does. Hence it isn't English or Computer as it's nothing.

Upvotes: 1

Related Questions