Automatic input validation

0

I am creating a login area and I had a question: I want to know if there is a way to verify in my database if the email entered in the registration field is already in use on my site, but I want this is done automatically (after the client type the login in the input a div from the print side whether it already exists or not, is there a way to do these types of automatic validation without the use of a submit?

    
asked by anonymous 17.11.2017 / 04:54

1 answer

0

For this you will need to use ajax and preferably jQuery:

JS:

$(document).ready(function () {
    // A cada tecla precionada executo o AJAX
    $("#id_do_campo_de_texto_onde_sera_inserido_o_email").keydown(function () {
        var request = $.ajax({
            url: "pagina_que_fara_a_busca_no_banco.php",
            method: "POST",
            data: {email: $("#id_do_campo_de_texto_onde_sera_inserido_o_email").val()}
        });

        request.done(function (data) {
           // data = será o retorno da página "pagina_que_fara_a_busca_no_banco.php"
           if(data == 1){
               // existe
           }else{
               // não existe
           }
        });

        request.fail(function () {
            console.log("OPS... ocorreu um erro na requisição");
        });
    });
});

PHP: page_que_fara_a_busca_no_banco.php

<?php

//coloque seu select aqui e verifique se o email existe

$sql = "SELECT... WHERE campo_email_banco like %".$_POST['email']."%"    
$email = "execute aqui a query";

if($email != ""){
    echo "1";
}else{
    echo "2";
}

?>

In the PHP file you use the search method you prefer, as I think very personal I did not write the correct code (I use class for example) ... But the important thing is to understand logic.

    
17.11.2017 / 11:23