How do I check real-time registration?


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.


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

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



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



    #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.
        echo json_encode(array('email' => 'Ja existe um usuario cadastrado com este email')); 
        echo json_encode(array('email' => 'Usuário valido.' )); 
asked by anonymous 09.08.2018 / 23:57

2 answers


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


 <script src=""></script><inputtype="text" id="email"/>
 <div id="resultados"></div>

<script type="text/javascript">

$(document).ready(function () {

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



$email = $_POST['email'];

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

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

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

    $emailBanco = $linha['email'];

    echo $emailBanco;
    echo "<br>";

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

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