How to solve an elseif that needs to be exclusive?

Asked

Viewed 75 times

6

I want to create a page where when a checkbox is marked, a message is printed, but if all checkbox are marked, print another message.

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <title>Form de exemplo com checkboxes</title>
        <meta charset="utf-8">
    </head>
    <body>
        <form action="" method="post">
            <p>
                <input type="checkbox" name="bike" value="on">eu tenho uma bike
            </p>
            <p>
                <input type="checkbox" name="car" value="on">eu tenho um carro
            </p>
            <p>
                <input type="submit" value="RLRLRLRL" />
            </p>
        </form>
    </body>
</html>
<?PHP
#$_POST['bike'] = ( isset($_POST['bike']) ) ? true : null;
#$_POST['car']  = ( isset($_POST['car']) )  ? true : null;
#var_dump($_POST);

$bike = $_POST['bike'];
$car = $_POST['car'];
if($bike) {
    echo "Olar, eu tenho uma bike";
} elseif ($car) {
    echo "Olar, eu tenho um carro";
} elseif ($car || $bike) {
    echo "Olar, eu tenho os dois";
}

?>
  • I have both, it means both, so it’s not one ou and yes a e(&&)

2 answers

5


In this particular case the first two conditions are wrong, because it must be executed only if it can be exclusive, then the condition has to ensure that it has nothing else. There is an error in the latter because it will print if you have either, and not both, solution:

if ($bike && !$car) {
    echo "Olar, eu tenho uma bike";
} elseif ($car && !$bike) {
    echo "Olar, eu tenho um carro";
} elseif ($car && $bike) {
    echo "Olar, eu tenho os dois";
}

I put in the Github for future reference.

There are other ways to solve this, but this is as close as you wrote.

  • And in the situation of having several items ? I will always have to list all of them and sort manually (!$) ?

  • Then the logic would probably have to be different, I answered what you asked, for another problem, the answer would be different. Maybe the solution is to do something very different. But it’s always complicated to say something if the requirements are not well defined - and they make sense, of course. Did you ever think the last sentence doesn’t make sense? "I have both" what? It doesn’t solve a real problem.

3

Since the values of checkboxs are the same, another way to solve this is to combine array_keys() to get all values keys(on) found, and check the amount with count().

//equivalente os checkboxs marcados pelo $_POST
$itens = ['bike' => 'on', 'car' => 'on'];
//$itens = ['bike' => 'on']; //outro teste
$marcados = array_keys($itens, 'on');

if(count($marcados) === 1 ){
    echo 'eu tenho '. $marcados[0];
}else if(count($marcados) === 2){
    echo 'tenho ambos, bike e carro';
}
  • 1

    I also have the picture of the dog now ;)

  • 1

    @Wallacemaxters wow Doge, such Shiba, very cutie. haha xD

  • @Wallacemaxters, the other day you were talking about array_keys() look at it there, in this case, only because the values of the checkboxs are equal then it works with the keys.

  • Cool, @rray. + 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.