Field Validation in html and php

0

I would like an help to validate a field in the form to check if there is already a same email that the user enters in the database. When the user clicks the button "register" will send direct to the page "sendCadastro.php" without any validation other than the bootstrap itself. I do not know if I validate the email field in own cadastro.php or in enviaCadastro.php. I am using PHP PDO to register in the database.

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

enviaCadastro.php

session_start ();    include_once 'connectBanco.php';

$ clicaCadastro = filter_input (INPUT_POST, 'clicCastastro', 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";

}
    
asked by anonymous 14.10.2018 / 04:04

2 answers

0

I think 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: link

    
14.10.2018 / 16:05
0

In the same way that you created the insert query you create one that performs the select, the structure using PDO stays 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.
    
15.10.2018 / 13:49