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?– rray
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.
– Sr. André Baill
Put it there on the date:"email=" + email.val()
– Gabriel Rodrigues
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()},
– rray
It didn’t work.. But I think you’re not even accessing the PHP file, because it doesn’t return anything..
– Sr. André Baill
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...
– Sr. André Baill
Remember to do the
$.parseJSON()
in date no js.– rray
I don’t know how you do it.
– Sr. André Baill
But the problem is already in PHP, because I even got a return... but I can’t use this to check
– Sr. André Baill
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.
– Sr. André Baill
The check is already being done in php, just send msg pro js.
data = $.parseJSON(data); $("#resposta").append(data.email)
. changedata.email
by the name q vc gave in json_enconde()– rray
I don’t understand. Where I put date = $.parseJSON(date); ?
– Sr. André Baill
Let’s go continue this discussion in chat.
– rray
It worked. Now I only handle PHP with database search. I really appreciate your help @rray
– Sr. André Baill
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.
– Sr. André Baill