String Comparison Problem in AJAX

Asked

Viewed 246 times

0

Good night, Personal to with a problem here, to using an ajax code to check if a Cpf is already registered without the refresh in the page not to lose the data, and returning a string with a value encrypted in sha1 for comparison and know if there is already register with Cpf in the bank, but ajax can’t compare the string, and if I from Alert in the result it prints the result variable right from php, I already searched and in American forums said it was only put $.Trim() but it didn’t help either, If anyone can help me or suggest anything, I’d appreciate it. 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';
    }
  }

2 answers

2


You’re not having a problem with comparison, you’re having a problem with feedback. Currently your request is asynchronous, so it makes no 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 spoil user interaction with the page.

Synchronous requests stop code execution, affecting the page around until it is finished. A request for this "pause" all while it is not finished. for (; xhr.readyState !== 4 ;);, is 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 so: return an instantiated object from an interface, where I hope to be defined as events done by phone call. But to make the code more basic I will leave a callback specified in a parameter of its function (now missing the request error callback).

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

    Thanks, I noticed yesterday also that the problem was in the return it was not returning so I changed the sequence of verification and stopped returning true or false to an If and put it in there direct

1

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

See, how your request is asynchronous the Returns within your ajax call on Success make no sense, for the following reasons:

  1. Closure principle (http://www.w3schools.com/js/js_function_closures.asp) your Return is in another scope and not in the scope of Function VerificaCPF
  2. Asynchronous call. When your ajax call receives the response the javascript execution scope has already passed the Function call VerificaCPF. therefore a var result = VerificaCPF() doesn’t 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 Function funcaoDeCallback in your HTML to make the appropriate feedback to the user.

Browser other questions tagged

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