Verify that the completed email already exists in the database without refresh

5

I need to fill in an email in the email field, search the database and return a message telling me if this email is already registered in the database. We developed jQuery + PHP according to the answers posted in a question that we sent, but in the same way, I could not elaborate.

VerifyEmail.html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputtype='text'id='email'><divid='resposta'></div><scriptlanguage="javascript">
    var email = $("#email");
        email.change(function() {
          $.ajax({
            url: 'verificaEmail.php',
            type: 'POST',
            data: email.val(),
            dataType: 'json',
            success: function(data) {
              console.log(data);
              if (data.email) { // se existir
                $("#resposta").append('Ja existe um usuario cadastrado com este email');
              }

            },
            error: function() {
              $("#resultado").show().fadeOut(5000);
            }
          });
        });
</script>

VerifyEmail.php

<?php
if ( isset($_POST['email']) ) {
    $vEmail = $_POST['email'];
    if ( $vEmail == "[email protected]" ) {
        return true;
    } else {
        return false;
    }
}
?>

The case is, that when running, does not return anything ... Nor does the console return errors ... Can anyone help me? Thanks!

    
asked by anonymous 26.07.2015 / 01:47

1 answer

8

Here is the solution with the help of @rray.

VerifyEmail.html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputtype='text'id='email'><divid='resposta'></div><scriptlanguage="javascript">
    var email = $("#email"); 
        email.blur(function() { 
            $.ajax({ 
                url: 'verificaEmail.php', 
                type: 'POST', 
                data:{"email" : email.val()}, 
                success: function(data) { 
                console.log(data); 
                data = $.parseJSON(data); 
                $("#resposta").text(data.email);
            } 
        }); 
    }); 
</script>

VerifyEmail.php

<?php
#Verifica se tem um email para pesquisa
if(isset($_POST['email'])){ 

    #Recebe o Email Postado
    $emailPostado = $_POST['email'];

    #Conecta banco de dados 
    $con = mysqli_connect("localhost", "root", "", "outrasintencoes");
    $sql = mysqli_query($con, "SELECT * FROM usuarios WHERE email = '{$emailPostado}'") or print mysql_error();

    #Se o retorno for maior do que zero, diz que já existe um.
    if(mysqli_num_rows($sql)>0) 
        echo json_encode(array('email' => 'Ja existe um usuario cadastrado com este email')); 
    else 
        echo json_encode(array('email' => 'Usuário valido.' )); 
}
?>
    
26.07.2015 / 03:03