Ajax query does not work

Asked

Viewed 114 times

2

Well, a simple ajax, php and mysql query. This same code changed only by field name works normally on another page.

$(document).ready(function(){
$('#email').keyup(function(){ 
var Email = $(this).val(); 
var EmailResult = $('#div_mensagem_valida_email'); 
if(Email.length > 3) { 
EmailResult.html('Verificando..'); 
var UrlToPass = 'action=email='+Email;
$.ajax({
type : 'POST',
data : UrlToPass,
url  : 'include/cadastra_novo_cliente_verifica.php',
success: function(responseText){ 
if(responseText == 1){
EmailResult.html('<span class="error">Ops, este e-mail já está cadastrado</span>');
}
else(responseText == 0){
EmailResult.html('');
}               
}
});
}else{
EmailResult.html('Aguardando...');
}
if(Email.length == 0) {
EmailResult.html('');
}
});

$('#email').keydown(function(e) { 
if (e.which == 32) {
return false;
}
});
});

The input is:

<input id="email" class="form-control input-lg" type="text" name="email" autocomplete="off" placeholder="Seu e-mail" required>

Just nothing happens, no Firebug returns me error

  • But in the Network tab (network) shows the request for the page include/cadastra_novo_cliente_verifica.php? In the console tab shows some syntax error of the front-end (javascript)?

  • var UrlToPass = 'action=email='+Email. What’s this? Action = Email = Email

  • @Guilhermenascimento, does not show, in the console appears only 'Syntaxerror: Missing ; before statement'

  • That is your problem SyntaxError: missing ; before statement' there is a syntax error in your javascript that will affect the functioning and nothing will run, remember syntax errors prevent the rest of the script they see from working... This error message says that a ; somewhere

1 answer

1


Here you forgot the elseif, see:

}
else(responseText == 0){

The correct is:

}
else if(responseText == 0){

I recommend doing a good code indentation too, should look something like:

$(document).ready(function() {
    $('#email').keyup(function() {
        var Email = $(this).val();
        var EmailResult = $('#div_mensagem_valida_email');
        if (Email.length > 3) {
            EmailResult.html('Verificando..');
            var UrlToPass = 'action=email=' + Email;
            $.ajax({
                type: 'POST',
                data: UrlToPass,
                url: 'include/cadastra_novo_cliente_verifica.php',
                success: function(responseText) {
                    if (responseText == 1) {
                        EmailResult.html('<span class="error">Ops, este e-mail já está cadastrado</span>');
                    } else if(responseText == 0) {
                        EmailResult.html('');
                    }
                }
            });
        } else {
            EmailResult.html('Aguardando...');
        }
        if (Email.length == 0) {
            EmailResult.html('');
        }
    });

    $('#email').keydown(function(e) {
        if (e.which == 32) {
            return false;
        }
    });
});

Tools like Sublimetext or online tools like http://jsbeautifier.org should help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.