Show button according to php parameter condition

Asked

Viewed 836 times

0

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

I tried that way but I couldn’t

<?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>
}
?>

So the button would only appear if the ($parametro == 1). It is possible to do this with the button ?

3 answers

3

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;

2

Just 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 
}
?>

  • Thank you, I managed to do it that way.

2


First take the parameter and already check if it was set. This is because the user (intentionally or unintentionally) can remove the parameter and this can 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 was used (which functions as a fast IF). If the value has been passed it is stored in the $param variable 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 the most appropriate IF notation for HTML has been used. Instead of spreading key locks in HTML you will have an endif that is much more friendly.

<?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; ?>
  • Wonderful, thank you so much for your help :)

Browser other questions tagged

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