Checking if login already exists via AJAX

0

Alright? I'm hoping that when I lose focus, the login field goes to the bank and looks for an equal login. If there is, it means that the guy has to use another and for that the return must be a mistake. If there is not a login like his, nothing happens and he continues to register at will.

My js code is:

function checalogin() {
        $.ajax({
        url: 'ajax-insere.php?login=' + $('input[name=login]').val(),
        type: 'GET',
        });
}

And the php code is:

    if(isset($_GET['login']) && ($_GET['login'] != '')) {
    $txtLogin = $_GET['login'];    
    $checa_login = 'SELECT * FROM usuarios WHERE login LIKE "' . $txtLogin . '"';
    //echo $checa_login;
    $resposta = mysql_query($checa_login) or die(mysql_error());
    $value = mysql_fetch_assoc($resposta);
    if ($value['login'] == $txtLogin) { 
        header('Location:visualiza.php?loginExiste=1');        
    }
}

Notice that the header returns with loginExiste = 1. I created a condition in php that:

if(isset($_GET['loginExiste']) && $_GET['loginExiste'] == 1) {
   echo '<script>'.
       'Materialize.toast('Já existe um login igual a esse. Tente algodiferente.', 7000);'.
   '</script>';
}

However, when you lose focus, that is, onBlur, nothing happens. What am I doing wrong?

Thank you!

    
asked by anonymous 10.08.2016 / 16:43

2 answers

2

You're mixing PHP with Javascript in a "wrong" way. It's not wrong, but what you expect as a result is different from what's actually going to happen.

The location will only affect AJAX, but will not trigger PHP for obvious reasons (PHP does not run on the client browser).

So what is actually occurring is:

AJAX -> ajax-insere.php?login=nome -> visualiza.php?loginExiste=1

PHP that exists in visualiza.php has no impact on the page, this will only be the return of AJAX, so you should treat such a return. So it's AJAX who gets <script>'Materialize.toast('Já existe um login igual a esse. Tente algodiferente.', 7000);</script> . A solution is .html() , so you will inject script into the page, but I do not find a solution as effective.

Best fix:

Javascript:

function checalogin() {
  $.getJSON('ajax-insere.php?login=' + $('input[name=login]').val(), function(json) {

    if (json['isValid'] === false) {
      Materialize.toast('Já existe um login igual a esse. Tente algo diferente.', 7000);
    }

  });

}
  

In this way the alert / toast will be displayed by the AJAX itself, with a true / false return from JSON.

PHP:

if(isset($_GET['login']) && !empty($_GET['login'])){

    $resposta = mysql_query('SELECT id FROM usuarios WHERE login = "' . mysql_real_escape_string($_GET['login']) . '"');
    $json['isValid'] = mysql_num_rows($resposta) > 0 ? false : true;

    echo json_encode($json);

}
  

In this way PHP will return true / false if there is already a SAME name as typed by the user.

ATTENTION:

mysql_ * is already deprecated (new versions no longer support this!) and it is not recommended to use!

    
10.08.2016 / 17:32
0

Have you tried marrying this blur action using jQuery?

$("#IdDoCampoQueVaiDarBlur").blur( function(){
    checalogin();
});
    
10.08.2016 / 17:23