String comparison problem in AJAX

0

Good evening, I have a problem with here, I'm using an ajax code to check if a cpf is already registered without the refresh on the page to not lose the data, and returning a string with a value encrypted in sha1 for comparison and know if there is already a registration with cpf in the database, but ajax can not compare the string, and if I do alert in the result it right up the result variable from php right, I already searched and in American forums said that it was only put $ .trim () but that did not help either, if anyone can help me or suggest something I appreciate. The codes:

Ajax:

function VerificaCPF(strCPF)
    {
      $.ajax({
        type: 'post',
        url: 'daoAssociado.php',
        data: {action: 'VerificaCPF', cpf: strCPF},
        success: function(resultado)
        {
          if(resultado == '88e9d785061a31f8bae950b8e231f40426b5496c')
          {
            return false;
          }
          else if (resultado == '4b2c518cb740685b1b29f477283165b732651f05')
          {
            return true;
          }
        }
      });
    }

php:

if(isset($_POST['action']) && !empty(($_POST['action'])))
  {
    $action = $_POST['action'];
    switch($action)
    {
      case 'VerificaCPF':
        consultarCPF($conectar);
      break;

      case 'VerificaUsuario':
        consultarUsuario($conectar);
      break;
    }
  }

function consultarCPF($conectar)
  {
    $cpf                    = sha1($_POST['cpf']);
    $queryVerificarCpf      = "exec usp_verificarExistenciaCpf @cpf = ?";
    $parameterVerificarCpf  = array($cpf);
    $query_Resultado        = sqlsrv_query($conectar, $queryVerificarCpf,      $parameterVerificarCpf) or die(header("Location:erronobanco.php"));
    $array_Resultado        = sqlsrv_fetch_array($query_Resultado);
    $totalDeCpf             = $array_Resultado['total'];

    if($totalDeCpf != 0)
    {
      echo '88e9d785061a31f8bae950b8e231f40426b5496c';
    }
    else
    {
      echo '4b2c518cb740685b1b29f477283165b732651f05';
    }
  }
    
asked by anonymous 13.09.2016 / 01:37

2 answers

2

You're not having a problem with comparison, but with feedback. Currently your request is asynchronous, hence it does not make sense for you to return data about it directly in the code. Another problem is that you do not return anything in the scope of VerificaCPF .

If your request was synchronous, you could get your information directly. Unfortunately or fortunately, synchronous requests will be removed in the future as they will spoil user interaction with the page.

Synchronous requests stop the execution of the code, affecting the page around until they are terminated. A request of these "pauses" all while it does not end. for (; xhr.readyState !== 4 ;); , it's like an infinite loop that breaks until the request is made, only this is something native.

What I would recommend you do is use functions like callbacks, passing extra arguments to them. I currently do this: I return an object instantiated from an interface, where I expect events to be defined as done per calls. But to leave the code more basic I will leave a callback specified in a parameter of your function (now the request error callback is missing).

function VerificaCPF(strCPF, callback) {
    $.ajax({
        type: 'post',
        url: 'daoAssociado.php',
        data: {
            "action": 'VerificaCPF',
            "cpf": strCPF
        }
    }).done(function(resultado) {
        var valid = '4b2c518cb740685b1b29f477283165b732651f05'
        callback(resultado === valid)
    })
}
    
13.09.2016 / 12:29
1

There are several ways you can solve this, however the problem you are facing is the scope of execution.

See, as your request is asynchronous the returns within your ajax call on success are meaningless for the following reasons:

  • Closure principle ( link ) your return is in another scope and not in the scope of function VerificaCPF
  • Asynchronous call. When your ajax call receives the response the execution scope of the javascript has already passed the call of the function VerificaCPF . so var result = VerificaCPF() does not work.
  • One possible solution is to use a callback function.

    For example:

    function funcaoDeCallback(resultado) {
          if(resultado == '88e9d785061a31f8bae950b8e231f40426b5496c')
          {
            return false;
          }
          else if (resultado == '4b2c518cb740685b1b29f477283165b732651f05')
          {
            return true;
          }
    }
    
    function VerificaCPF(strCPF, callBack)
    {
      $.ajax({
        type: 'post',
        url: 'daoAssociado.php',
        data: {action: 'VerificaCPF', cpf: strCPF},
        success: function(resultado)
        {
          callBack(resultado);
        }
      });
    }
    
    VerificaCPF('12345678912', funcaoDeCallback);
    

    So you can use the funcaoDeCallback function in your HTML to make proper feedback for the user.

        
    13.09.2016 / 14:07