Form gives error in PHP

1

I'm trying to make the form on my page return an error and stay on the page if no field is filled.

However, he is doing this even though all fields are filled out correctly! Could you help me?

The form page:

    <body>
        <div class="container container-twelve">
            <div class="four columns offset-by-four">
                <h1 class="titles">Cadastro</h1>
                <?php if(isset($_SESSION["success"])) {?>
                    <p><?= $_SESSION["success"] ?></p>
                <?php }?>
                <?php unset($_SESSION["success"]); ?>
            </div>
            <div class="four columns offset-by-four" id ="login">
                <?php if (isset( $_GET['erro'] )) {
                    echo '<div class="erro">';
                    echo htmlentities( $_GET['erro'] );
                    echo "</div>\n";
                } ?>
                <form action="cadastra_usuario.php"  method="post">
                    <label for="nome">Nome</label>
                    <input type="text" name="nome" placeholder="Digite seu nome">
                    <label for="email">Email de usuário </label>
                    <input type="text" name="email" placeholder="Seu email para login">
                    <label for="senha">Senha</label>
                    <input type="password" name="senha" placeholder="Sua senha">
                    <label for="senha2">Repita sua senha</label>
                    <input type="password" name="senha2" placeholder="Repita sua senha">
                    <input type="submit" value="Cadastrar">
                </form>

                <p><a href="index.php"> << Voltar para o site</a></p>
                <p><a href="login.php"> Já tenho um cadastro >> </a></p>
            </div>
        </div>
    </body>
    </html> 

or PHP:

    <?php 
        include('conecta.php');
        include('functions.php');

        $senha  = $_POST['senha'];
        $senha2 = $_POST['senha2'];
        $nome   = $_POST['nome'];
        $email  = $_POST['email'];

        if($senha != $senha2){
            $erro = urlencode( 'As senhas não conferem!' );
            header("Location: cadastro.php?erro=$erro");
            die();
        }

        $verifica = verificaCadastra($nome, $email, $senha);
        if( $verifica == null){
            $erro = urlencode( 'Por favor, preencha todos os campos!' );
            header("Location: cadastro.php?erro=$erro");
            die();
        }

        $_SESSION["success"] = "Usuário cadastrado com sucesso.";
        header("Location: login.php");
    ?>

a function:

    function verificaCadastra($nome, $email, $senha){
            if(empty($nome)){
                $nome == null;
            }
            if(empty($email)){
                $email == null;
            }
            if(empty($senha)){
                $senha == null;
            }
        }
    
asked by anonymous 08.12.2014 / 03:39

1 answer

4

Your problem is here: $verifica = verificaCadastra($nome, $email, $senha);

This function does not have return , that is, even if everything works fine, it will not return, and the $verifica variable will have NULL value. You need to add a return in the function. Note that within the function the variables $nome , $senha and $email are not the same as outside it. In other words, trying to undo the value with $senha == null; will not produce result out of function ...

I suggest using this:

function verificaCadastra($nome, $email, $senha){
    if(empty($nome) || empty($email) || empty($senha)) return false;
    return true;
}
    
08.12.2014 / 07:14