Field Validation in html and php

Asked

Viewed 158 times

0

I would like a help to validate a field in the form to check if there is already a same email that the user type in the database. When the user clicks on the "register" button he will send directly to the "send.php" page without any validation other than the bootstrap itself. I don’t know if I validated the email field in the.php register itself or in the uploadCadastro.php. I am using PHP PDO to register in the database.

php.

    <form action="enviaCadastro.php" method="POST" class="needs-validation" novalidate>
          <div class="col-md mb-3">    
                  <input type="email" placeholder="E-mail" id="email" name="email" class="form-control" maxlength="30" required >
                  <div class="invalid-feedback">Preencha com um e-mail</div><br>
          </div>
    </form>

envieCadastro.php

session_start(); include_once 'connects Anco.php';

$clicaCadastro = filter_input (INPUT_POST, 'clicaCadastro', FILTER_SANITIZE_STRING);

if($clicaCadastro){
    //Pega os dados do form
    $nome = filter_input (INPUT_POST, 'nome', FILTER_SANITIZE_STRING);
    $sobrenome = filter_input (INPUT_POST, 'sobrenome', FILTER_SANITIZE_STRING);
    $datanasc = filter_input (INPUT_POST, 'datanasc', FILTER_SANITIZE_STRING);
    $genero = filter_input (INPUT_POST, 'genero', FILTER_SANITIZE_STRING);
    $estado = filter_input (INPUT_POST, 'estado', FILTER_SANITIZE_STRING);
    $cidade = filter_input (INPUT_POST, 'cidade', FILTER_SANITIZE_STRING);
    $email = filter_input (INPUT_POST, 'email', FILTER_SANITIZE_STRING);
    $senha = filter_input (INPUT_POST, 'senha', FILTER_SANITIZE_STRING);

    //insere no banco de dados
    $envia = "INSERT INTO cadastro (nome, sobrenome, nasc, genero, estado, cidade, email, senha) VALUES (:nome, :sobrenome, :datanasc, :genero, :estado, :cidade, :email, :senha)";

    $insere_bd = $conecta->prepare($envia);
    $insere_bd ->bindParam(':nome', $nome);
    $insere_bd ->bindParam(':sobrenome', $sobrenome);
    $insere_bd ->bindParam(':datanasc', $datanasc);
    $insere_bd ->bindParam(':genero', $genero);
    $insere_bd ->bindParam(':estado', $estado);
    $insere_bd ->bindParam(':cidade', $cidade);
    $insere_bd ->bindParam(':email', $email);
    $insere_bd ->bindParam(':senha', $senha);

    if($insere_bd->execute()){
        header("Location: cadastroRealizado.php");
    }else{
        header("Location: cadastroErro.php");
    }


}else{
    $_SESSION ['erro'] = "<p style='color:red;'>Cadastro nao efetuado</p>";
    echo "erro ao cadastrar";

}

2 answers

0

In the same way that you created the Insert query you create one that performs select ,the structure using PDO is in this schema:

  $sql = ("SELECT id FROM cadastro WHERE email=:email");
        $teste = $conn->prepare($sql);
        $teste->bindParam(":email", $email);
        $teste->execute();
        $has = $teste->fetch(PDO::FETCH_OBJ);

        //Caso $has seja diferente de nulo significa que o email já existe.

0

I believe this function can help you:

    public static function EmailValidate(string $val) : bool
    {
        return (bool)filter_var($val, FILTER_VALIDATE_EMAIL);
    }

This is one of the functions of my validation class: https://github.com/hax0rlib/xSecurity

  • in case this validation already exists, I wanted one to check if there is already an email registered in the database according to the email that the user put.

Browser other questions tagged

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