Checking if login already exists via AJAX

Asked

Viewed 73 times

0

All right? I’m hoping that when you lose focus, the login field will go to the bank and look for an equal login. If there is, it means the guy has to use another and for that the return must be a mistake. If there is no login like his, nothing happens and he continues to register at will.

My code js 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');        
    }
}

Note 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 I lose focus, that is, onBlur, nothing happens. What I am doing wrong?

Thank you!

2 answers

2


You are mixing PHP with Javascript in a "wrong" way. It is not wrong, but what awaits as a result is different from what will actually occur.

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

So what’s actually happening is:

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

The PHP that exists in visualiza.php has no impact on the page, it will only be the return of AJAX, so you should treat such return. So it is AJAX that receives the <script>'Materialize.toast('Já existe um login igual a esse. Tente algodiferente.', 7000);</script>. One solution is the .html(), this way will inject on the page the script, but I don’t find such an effective solution.

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);
    }

  });

}

This way the/Oast alert will be displayed by AJAX itself, with a return of true/false 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);

}

This way PHP will return true/false if there is already a name EQUAL to the one typed by the user.

ATTENTION:

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

  • Dude, I totally understood where I was going wrong, or rather confusing. I made the changes and even changed a little but I’m still having problems. json ALWAYS returns {"isValid":true}. I tried to echo the query and it does not appear. $_GET['login'] comes. But I don’t think you’re rolling the query... you don’t think so either?

  • 1

    You may need to change some things. : S Try to separate, as before, the mysql_query then give echo. So test in Phpmyadmin (or any Mysql manager). I don’t know if this is what you did. PHP will return true if there is no data login = $_GET['login'], before you used the LIKE, what is different! Try removing the mysql_real_escape_string. You can also create a $json['Error'] = !$resposta; this will return "Error":true if the mysql_query not executed.

  • SHOW, It worked. I separated the query and assigned a variable the value of mysql_real_escape_string . I didn’t even have to touch the JS. !

0

already tried to marry this Blur action using jQuery?

$("#IdDoCampoQueVaiDarBlur").blur( function(){
    checalogin();
});

Browser other questions tagged

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