If inside a Success in ajax

Asked

Viewed 506 times

0

I make the request, I get the right amount, but at the time of the check it’s not right

                $.ajax({
                    url:'includes/checkCPF.inc.php',
                    method:'POST',
                    type:'POST',        
                    data:{cpf:cpf},
                    success:function(data){
                        alert(data);
                    if(data == "V")
                    {
                        alert("b");   
                        $(".cpf").addClass("ShadowRed");
                        $(".cpf").focus();
                        return
                    }
                 });

php:

<?php
include_once '../acesso.php';
$cpf = $_POST["cpf"];
$sql = "SELECT * FROM login_usuario WHERE CpfUsuario = '$cpf'";
$pedido = $conn->query($sql);
if(mysqli_num_rows($pedido) != 0)
{
    echo "V";
}
else
{
    echo "F";
}
  • The Alert returns what?

  • V or F based on php’s if

1 answer

3


The return of Ajax with echo to receive a string can return blank spaces at the ends of the string, and this will make a difference in the if that will differentiate the letter V of a V with spaces before and/or after.

Clear these returned spaces on data with the method .trim():

if(data.trim() == "V")

Also missed closing the success: with keys }:

$.ajax({
   url:'includes/checkCPF.inc.php',
   method:'POST',
   type:'POST',        
   data:{cpf:cpf},
   success:function(data){
      alert(data);
      if(data.trim() == "V")
      {
         alert("b");   
         $(".cpf").addClass("ShadowRed");
         $(".cpf").focus();
         return;
      }
   } ← FECHAR AQUI
});
  • 1

    YOU SAVED MY LIFE!!!

  • Well used the method trim(). +1

Browser other questions tagged

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