Doubt in IF - PHP

Asked

Viewed 55 times

1

I’m having a question on the condition I’m willing to put in my system. If the ID the owner of the ad is equal to the one in the session a "accept negotiation" button appears, if not, the "Product on the way" button appears. When testing appears only the button "Accept negotiation".

Follows the code:

<?php
$id_usuario_anuncio = $_GET["ID_usuario"];
$id_usuario_sessao = $_SESSION["id"];
if($id_usuario_anuncio == $id_usuario_sessao)
{
   echo '<form method="post" action="">
   <input type="hidden" name="ATIVAR/EXCLUIR" value="<?php echo $_GET["ID"]; ?>">
   <div class="panel-footer">
   <button type="submit" name="ativar" id="ativar" value="ATIVAR"class="botao_cardapio">Aceitar negocição</button> 
   </div>
   </form>';
}
else
{
   echo '<form method="post" action="">
   <input type="hidden" name="ATIVAR/EXCLUIR" value="<?php echo $_GET["ID"]; ?>">
   <div class="panel-footer">
   <button type="submit" name="excluir" id="excluir" value="EXCLUIR"class="botao_cardapio">Produto a caminho</button> 
   </div>
   </form>';
}
?>
  • (I made a comment but deleted because I was not coming to the case)... If you are always entering the first condition, it is because the values are equal. Your problem is coming from elsewhere. SESSION may be empty and $_GET as well.

1 answer

1

Your problem is with the syntax that is totally incorrect. See examples on line 7 and line 16. You have opened tag PHP within a tag PHP. So the resolution is simple:

<?php
$id_usuario_anuncio = $_GET["ID_usuario"];
$id_usuario_sessao = $_SESSION["id"];


if($id_usuario_anuncio == $id_usuario_sessao)
    {
    echo '<form method="post" action="">
    <input type="hidden" name="ATIVAR/EXCLUIR" value="'.$_GET["ID"].'">
    <div class="panel-footer">
    <button type="submit" name="ativar" id="ativar" value="ATIVAR" class="botao_cardapio">Aceitar negocição</button> 
    </div>
    </form>';
    }
else
    {
    echo '<form method="post" action="">
    <input type="hidden" name="ATIVAR/EXCLUIR" value="'.$_GET["ID"].'>
    <div class="panel-footer">
    <button type="submit" name="excluir" id="excluir" value="EXCLUIR" class="botao_cardapio">Produto a caminho</button> 
    </div>
    </form>';
    }
?>

But if you allow me, I created a system a little simpler so that you do not double the size of your file (What implies the bandwidth when hosting):

<?php
$ua = $_GET["ID_usuario"];
$us = $_SESSION["id"];

    if ($ua == $us) {
        $text  = "Aceitar negociação";
        $name_id = "ativar";
    } else {
        $text  = "Produto a caminho";
        $name_id = "excluir";
    }
    $value = strtoupper($name_id); // retorna "ACEITAR"

?>

<form method="post" action="">
    <input type="hidden" name="ATIVAR/EXCLUIR" value="<?php echo $_GET['ID'] ?>">
    <div class="panel-footer">
    <button type="submit" name="<?php echo $name_id ?>" id="<?php echo $name_id ?>" value="<?php echo $value ?>" class="botao_cardapio"><?php echo $text ?></button> 
    </div>
</form>

Browser other questions tagged

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