Function Empty returns true (truth) when value is 0

Asked

Viewed 281 times

2

Hello, I am using the code below to save the status ($status_prod) of the button (Enabled? Yes or No), and also save the target address ($link_prod) of the button when enabled. But it’s only recording the YES option, and I’m not finding out how to make it record the NO option when selected.

I noticed when I insert this line }else if(isset($_POST['status_prod'])){, I can record the option NO.

And when I change to this line if(!empty($status_prod)) {, save the YES option.

I am posting the addresses so that friends can check the problem that is occurring.

Address of the panel where you changed the status of the button: http://www.simuleseusite.com/admin/

Access with (Login - user) and (Password - 123)

Go to (menu/enable button) Do the test by switching from Yes to No, and try to unlock it, and you will see that you are not saving the option Yes.

Address of Enable button result or not: http://www.simuleseusite.com/

Follow code used below:

<?php
//pega o valor do botao
        if(isset($_POST['status_prod'])){ // só entra aqui, se gale_status tiver sido postado
                $link_prod = null;
                $status_prod = 'Não';

// se a pessoa marcar a opção sim:
        if($_POST['status_prod'] == "1") {
                $link_prod = '<li><a href="'.$end.'prod_index.php" class="nav1">'.$bot_prod.'</a></li><li class="divider"></li>';
                $status_prod = 'Sim';

        //}else if(isset($_POST['status_prod'])){ // Com essa linha funciona a opção NÃO
        if(!empty($status_prod)) {              // Com essa linha funciona a opção SIM

// Se existir sessão, eu crio aqui
        $_SESSION['hab_prod']['status_prod']=$status_prod;
        $_SESSION['hab_prod']['link_prod']=$link_prod;

        //header("location:menu_halitar_link.php");

        echo "<meta http-equiv='refresh' content='0; URL= menu_halitar_link.php'>
        <script language='javascript'>
        window.alert('Dados atualizados com sucesso!');
        </script>";
        }}} // Retirar uma Chave se for usar a opção NÃO
?>
        <form method="post">
            <label>Habilitar o Link <?php echo $bot_prod ?>?</label><br><br>
            <input type='radio' name='status_prod' value='1' checked="checked"/><label>Sim</label>
            <input type='radio' name='status_prod' value='0'><label>Não</label>
            <input type="submit" value="Atualizar">
        </form>

If friends can help me to find out where I’m going wrong I’d be very grateful.

Fight to all, and big hug.

2 answers

3


I’ve been running some tests and taking this under consideration:

I have come to the conclusion that the whole value 0 and the string "0" are considered empty for the method empty(), returns true. In this example below choose "No" (value="0") will not enter the first condition, while if you choose the "yes" (value="1") enters both

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(!empty($_POST['status_prod'])) {
        echo 'Não está vazio: ' .$_POST['status_prod'];
    }
    if(isset($_POST['status_prod'])) {
        echo 'Existe: ' .$_POST['status_prod'];
    }
}
?>
<form method="post">
    <label>Habilitar o Link</label><br><br>
    <input type='radio' name='status_prod' value='1' checked="checked"/><label>Sim</label>
    <input type='radio' name='status_prod' value='0'><label>Não</label>
    <input type="submit" value="Atualizar">
</form>

We can check this out if:

var_dump(empty(0)); // bool(true)
var_dump(empty("0")); // bool(true)
var_dump(empty(1)); // bool(false)
...

By HTML some button value radio will always be sent because sim will always get checked, you can get around it in the following way:

if(isset($_POST['status_prod']) && $_POST['status_prod'] == 0) {
    // input "Não", $_POST['status_prod'] = 0
}
else {
    // input é sim
    $_POST['status_prod'] = 1; // inserir esta linha por segurança
}

PS: In principle one of the values will always be sent, because your "Yes" starts with checked, but we have ALWAYS do the server side checks

1

With the help of my friend Miguel who gave me a light so that I could reach my goal, which is to be able to record the Status of the button and enable it according to the YES or NO selection.

Access the addresses mentioned above to test and check the result.

Below perfect working code.

<?php
if(isset($_POST['status_prod'])) {

if(isset($_POST['status_prod']) && ($_POST['status_prod']) == 0) { // Desabilita o Botão.
    $link_prod = null;
    $status_prod = 'Não';
}else{
    $_POST['status_prod'] == 1; // Habilita o Botão com o Link de destino.
        $link_prod = '<li><a href="'.$end.'prod_index.php" class="nav1">'.$bot_prod.'</a></li><li class="divider"></li>';
        $status_prod = 'Sim';
        }
        $_SESSION['hab_prod']['status_prod'] = $status_prod;
        $_SESSION['hab_prod']['link_prod'] = $link_prod;

        echo "<meta http-equiv='refresh' content='0; URL= menu_halitar_link.php'>
        <script language='javascript'>
        window.alert('Dados atualizados com sucesso!');
        </script>";
        }
?>
        <form method="post">
            <label>Habilitar o Link <?php echo $bot_prod ?>?</label><br><br>
            <input type='radio' name='status_prod' value='1' checked="checked"/><label>Sim</label>
            <input type='radio' name='status_prod' value='0'/><label>Não</label>
            <input type="submit" value="Atualizar">
        </form>

I appreciate the attention of all friends to my problem, which with the help of friend Miguel was solved.

Hugs to all.

  • I’m glad you decided

Browser other questions tagged

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