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