Function empty returns true when value is 0

1
Hello, I am using the code below to write the status ($ status_prod) of the button (Enabled? Yes or No), and also record the destination ($ link_prod) address of the button when enabled. But it is only writing the YES option, and I can not figure out how to make it save the NO option when selected.

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

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

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

Address of the panel where you changed the status of the button: link

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

Enter (menu / enable button) Take the test by switching from Yes to No and try to shred it, and you will see that you are not writing the Yes option.

Button Enable Result Result: link

Here is the 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 find out where I'm wrong I'll be very grateful.

Brigadão to all, and big hug.

    
asked by anonymous 02.06.2016 / 12:43

2 answers

2

I've been testing and taking this into consideration :

I came to the conclusion that the integer value 0 and the string "0" are considered empty for the empty() method, returns true . In this example, if you choose "no" ( value="0" ), you will not enter the first condition, while choosing "yes" ( value="1" ) will enter the two

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 value of the radio button will always be sent, since sim will always start checked, you can work around this as follows:

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, since your "Yes" starts with checked , but we ALWAYS have to do the server side checks

    
02.06.2016 / 13:11
0

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 Button Status and enable it according to the YES or NO selection.

Access the addresses mentioned above to test and verify 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 thank all the friends for their attention to my problem, which with the help of my friend Miguel has been solved.

Hugs to everyone.

    
02.06.2016 / 17:22