Form gives error constantly in PHP

Asked

Viewed 278 times

1

I am trying to make my page form return an error and remain on the page if no field is filled in.

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

The page of the form:

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

the 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");
    ?>

the Function:

    function verificaCadastra($nome, $email, $senha){
            if(empty($nome)){
                $nome == null;
            }
            if(empty($email)){
                $email == null;
            }
            if(empty($senha)){
                $senha == null;
            }
        }

1 answer

4


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

That function does not have return, that is, even if everything works well it will not give any return and the variable $verifica will have value NULL same. You need to add a Return to the function. Note that within the function the variables $nome, $senha and $email are not the same as outside it. That is to try to undo the value with $senha == null; will not produce result outside the function...

I suggest using like this:

function verificaCadastra($nome, $email, $senha){
    if(empty($nome) || empty($email) || empty($senha)) return false;
    return true;
}
  • Thanks my friend ! It worked perfectly !

Browser other questions tagged

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