The goal is to display one link or another based on the value of a variable sent by get.
- On page reload if there is no "value" request, $value is FALSE. As value is false the high link has visible class and the Cancel link has hidden class
- When clicking on Add, we send by get true as the value of "value" and when reloading the page the Add link is hidden and the Cancel link is visible
- When clicking Cancel, we send by get false as value of "value" and when reloading the page, although the $value is false, THE CANCEL LINK STILL APPEARS WITH CLASS VISIBLE AND THE HIGH LINK AS HIDDEN
QUESTION:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<style>
.oculto{
display:none;
}
.visible{
display:block;
}
</style>
<title>Document</title>
</head>
<body>
<?php
$valor = isset($_REQUEST["valor"]) ? $_REQUEST["valor"]:false;
print $valor." valor";
?>
<a href="get.php?valor=true" class=<?php echo $valor ?
'oculto':'visible'?>>Alta</a>
<a href="get.php?valor=false" class=<?php
echo $valor ? 'visible':'oculto'?>>Cancelar</a>
</body>
</html>
In your Cancel link you are assigning 'false' to the value parameter. But, beware, when you receive the parameter, either using $_GET or $_REQUEST, what you receive is not the boolean value false, but a data of type string, which is 'false'.
Therefore, you would be assigning the variable $false the value 'false' (string, not boolean).
When you evaluate $value to paint 'visible' or 'hidden', you evaluate it expecting it to be boolean data, but it is a string. And all strings except '', '0', ' ' and I don't know if any else evaluate to true. Therefore, your 'false' is true, and so it is painted as 'visible', leading to confusion.
One solution is to cast the string data to bool, but normally what is done is to pass the values 0 and 1 instead of false and true. I put the only thing that should be changed:
Note: I have also added quotes to the values of the class properties.