Automatic validation of inputs

Asked

Viewed 103 times

0

I am creating a login area and came to me a question: I wonder if there is a way to check in my database if the email entered in the registration field is already in use on my site, but I want this to be done automatically (after the client enters the login in the input a div of the print side if it already exists or not, there is a way to do these types of automatic validation without the use of a Ubmit ?

  • Create a Rest service that seeks to validate the email while completing the registration.

  • 1

    It doesn’t make sense to be in the login, it wouldn’t be in the register?

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: pagina_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 did not write the code straight (I use class for example)... But the important thing is to understand the logic.

  • Yes I understood perfectly, da para implementar um beforesend com a gif durante os requisições certo ? Because I thought to execute the request only after the character @ is placed because it is a little unnecessary so many requests, while this character is not placed the gif is running until it is placed or until the input loses focus.

  • @Studtephp in this case is better when the field loses focus, with the @ becomes too vague, there may be guilherme@gmail and guilherme@hotmail both valid but not repeated.

  • truth then my logic there would not work :(

  • however the ideal seems to me to try to make the request after a time the user stops typing

Browser other questions tagged

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