I'm having trouble making a request via ajax. The idea is that when checking out the email field a check will be done in the database if there is an equal registered email, but if I use the "dataType: 'json'" it does not work. It only works with "dataType: 'html'". Thanks if anyone can tell me why.
index.php:
<!DOCTYPE html>
<html>
<head>
<title>Consulta de Email</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type='text/javascript' src="jquery-1.11.1.min.js"></script>
<script type='text/javascript' src='email.js'></script>
</head>
<body>
<form id="form1" class="form1" method="post">
<label>email:</label><br />
<input type="text" name="email" id="email"/>
<br /><br />
<label>senha:</label><br />
<input type="text" name="senha" id="senha"/>
<br /><br />
<input type="submit" value="Salvar Dados" />
</form>
</body>
</html>
email.js:
$(document).ready( function() {
$('#email').blur(function(){
$.ajax({
url : 'consultar_email.php',
type : 'POST',
data: 'email=' + $('#email').val(),
dataType: 'json',
success: function(data){
if(data.cadastrado == true){
alert("email já cadastrado");
}
else{
alert("email não cadastrado");
}
}
});
return false;
})
});
consult_email.php
<?php
include_once("Classes/dadosDoBanco.php");
$cliente = new DadosCliente();
$email = $_POST['email'];
$sql = "SELECT * FROM cliente";
$totalReg = $cliente->totalRegistros($sql);
for($i=0;$i<$totalReg;$i++)
{
$cliente->verDados($sql, $i);
$email2 = $cliente->getEmail();
if($email == $email2)
{
$dados['cadastrado'] = true;
}
else
{
$dados['cadastrado'] = false;
}
}
echo json_encode($dados);
?>