4
I’m having trouble making a request via ajax. The idea is that when leaving the email field a check is made in the database whether or not there is a registered email equal, but if I use "dataType: 'json'" it does not work. Fuciona only with "dataType: 'html'". I’d appreciate it if someone could 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;    
   })
});
consultar_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);
?>
On the date line: 'email=' + $('#email'). val(). Change to date: {'email': $('#email'). val()}
– touchmx
@touchmx the way it was working, what’s giving problem is dataType: 'json', I just switched to dataType: 'html' and it worked right, would tell me the reason?
– Vinícius
If you give a
alert(data.cadastrado)what is the return?– Papa Charlie
the problem is that it does not get to go through the $.ajax part, and does not call the other file.
– Vinícius
Your code does not appear to be an error. On your page consultar_email.php let alone
echo json_encode( array('cadastrado' => false) );to see if there are any errors escaping in PHP before returning json.– Papa Charlie
does not present, I have tested this part without calling it through ajax, and it works as should
– Vinícius
@Vinicius has already tested
data: {email: $('#email').val(),instead ofdata: 'email=' + $('#email').val(),as has been suggested here?– Sergio
@Sergio yes, I’ve tried it, but this way it doesn’t work
– Vinícius