How do I check real-time registration?

6

I have a registration system that I need to verify in real time if the email exists in the database.

But the same is not working, in <div id="resposta"></div> does not display any result.

JavaScript

<script language="javascript">
document.addEventListener("DOMContentLoaded", function(){

    var email = $("#email"); 
        email.blur(function() { 
            $.ajax({ 
                url: 'plano.php', 
                type: 'POST', 
                data:{"email" : email.val()}, 
                success: function(data) { 
                console.log(data); 
                data = $.parseJSON(data); 
                $("#resposta").text(data.email);
            } 
        }); 
    }); 

        });
</script>

HTML

<input type="email" name="email" required="required" placeholder="Email">
<div id="resposta"></div>

plano.php

if(isset($_POST['email'])){ 

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

    #Conecta banco de dados 
    $sql = mysqli_query($con, "SELECT email FROM conta WHERE email = '{$emailpostado}'");

    #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.' )); 
}
    
asked by anonymous 09.08.2018 / 23:57

2 answers

6
  

Why instead of returning JSON you do not do a simple echo?

     

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><inputtype="text" id="email"/>
 <div id="resultados"></div>

<script type="text/javascript">

$(document).ready(function () {

    $('#email').blur(function() {
        $th = $(this);
          $.ajax({
            url: 'plano.php',
            type: 'POST',
            data: {email: $th.val()},
            beforeSend: function(){
                $("#resultados").html("Carregando...");
            },
            success: function(data){
                $("#resultados").html(data);
            },
            error: function(){
                $("#resultados").html("Ouve um erro ao enviar sua URL");
            }
         });//ajax       
    });
});

</script>

plano.php

$email = $_POST['email'];

$con = new PDO('mysql:host=localhost;dbname=NOME_DB',"USUARIO","SENHA");

    $sql = $con->prepare("SELECT email FROM conta where email='$email'");
    $sql->execute();

    $linha = $sql->fetch(PDO::FETCH_ASSOC);

    $emailBanco = $linha['email'];

    echo $emailBanco;
    echo "<br>";

    if(isset($email ) && $emailBanco === $email ){
        echo "Este email existe no banco";
    }else{
        echo "Email digitado " .$emailBanco. " Nao existe no banco "; 
    }
    
10.08.2018 / 01:13
3

The problem is that you are trying to get the "email id" that does not exist. This line generates an error:

var email = $("#email");

You need to enter the id in your <input/> :

<input id="email" type="email" name="email" required="required" placeholder="Email">

If you want to get the value of name . You can get it like this:

var email = $("input[name='email']");

When unexpected things like this happen, you can press F12 to go to console and check for errors that are occurring.

    
10.08.2018 / 00:58