5
I have a registration system that I need to check in real time if the email exists in the database.
But the same is not working, in the <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.' ));
}
Why instead of returning JSON you do not make a simple
echo
?– Sam
Andrei’s answer is correct, but you can take the
name
if you don’t want to add aid
in the field:var email = $("input[name='email']");
.– Sam
@Sam I’ll put it this way that you wrote.
– Andrei Coelho
As was put in the answer of leo and quoted by sam, in his case, there is no need to generate a JSON on the client side, nor the server side, as it is just a phrase.
– Andrei Coelho