Check if the completed email already exists in the database without refresh

Asked

Viewed 15,283 times

5

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

checkEmail.html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='text' id='email'>
<div id='resposta'></div>

<script language="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>

verificaEmail.php

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

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

  • when you access verificaEmail.php direct appears something? , from a print_r in $_POST?

  • Didn’t get to run the PHP file, I think... because I gave a print_r in the post and didn’t return anything... nor the array(), I also tried echo "test"; and didn’t return anything either.

  • 1

    Put it there on the date:"email=" + email.val()

  • 1

    From one check on these items, for each typed letter you send an ajax(onchange)? data: email.val(), passes the name of the sent field I believe it to be so: data:{"email" : email.val()},

  • It didn’t work.. But I think you’re not even accessing the PHP file, because it doesn’t return anything..

  • I did it like this: $output = $_POST['email']; echo json_encode($output); and I noticed on the console, passed the typed email, but if I return TRUE, it should theoretically tell me that the email exists.. And it doesn’t...

  • Remember to do the $.parseJSON() in date no js.

  • I don’t know how you do it.

  • But the problem is already in PHP, because I even got a return... but I can’t use this to check

  • If I put: echo json_encode($_POST['email']);, it returns the completed email. I would like to check, how is it done? It can be a simple if.. without the database, then I adjust the db.

  • The check is already being done in php, just send msg pro js. data = $.parseJSON(data); $("#resposta").append(data.email). change data.email by the name q vc gave in json_enconde()

  • I don’t understand. Where I put date = $.parseJSON(date); ?

  • It worked. Now I only handle PHP with database search. I really appreciate your help @rray

  • OK worked, however, if I type the email that is there appears the message... But if I type another email, the message goes on, and it doesn’t make a new existence check.

Show 10 more comments

1 answer

8


Here is the solution with the help of @rray.

checkEmail.html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='text' id='email'>
<div id='resposta'></div>

<script language="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>

verificaEmail.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.' )); 
}
?>

Browser other questions tagged

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