Appear button according to the condition of the php parameter

0

I wonder if it is possible to make the button appear only if the parameter is true. By the URL comes the parameter ../usuario?id=1&parametro=1

I tried this way but could not.

<?php 

$parametro = $_GET['Parametro'];

if ($parametro == 1){
<button  type="submit" class="btn btn-info btn-rounded"> <a href="editando_usuario?id=<? echo $_GET['id']?>" style="color: #ffffff"><i class="fa fa-pencil"></i>  Editar</a></button>
}
?>

Then, the button would only appear if ($parametro == 1) . Is it possible to do this with the button?

    
asked by anonymous 02.01.2018 / 16:47

3 answers

2

First receive the parameter and make sure it is set. This is because the user (intentionally or unintentionally) can remove the parameter and this may trigger an error or cause inappropriate behavior in the application (I recommend searching for Defensive Programming, XSS, SQL Injection and Tamper Data).

In this case a ternary operator (which functions as a fast IF) was used. If the value has been passed it is stored in the variable $ param and if it has been omitted from the URL the value zero is written to the variable.

<?php $param = isset($_GET['parametro']) ? $_GET['parametro'] : 0; ?>

Then in your HTML check if the parameter is equal to one. Note also that an IF notation most appropriate for HTML was used. Instead of spreading key locks in HTML you will have an endif that is much friendlier.

<?php if($param == 1): ?>
    <button  type="submit" class="btn btn-info btn-rounded"> <a href="editando_usuario?id=<? echo $_GET['id']?>" style="color: #ffffff"><i class="fa fa-pencil"></i>  Editar</a></button>
<?php endif; ?>
    
02.01.2018 / 18:32
2

With ternary operator:

$button = $_GET['parametro']==1 ? '<button  type="submit" class="btn btn-info btn-rounded"> <a href="editando_usuario?id="'.$_GET['id'].'" style="color: #ff0000"><i class="fa fa-pencil"></i>  Editar</a></button>' : "";
echo $button;
    
02.01.2018 / 17:30
1

It's just you break your php block or else write html from the snippet as a string.     

$parametro = $_GET['Parametro'];

if ($parametro == 1){
?>
     <button  type="submit" class="btn btn-info btn-rounded">
         <a href="editando_usuario?id=<? echo $_GET['id']?>" style="color: #ffffff"><i class="fa fa-pencil"></i>Editar</a>
     </button>
<?php 
}
?>
    
02.01.2018 / 16:53