0

I have something like this, where it is a simple call to a script that gives me back a value, a string but when i use alert function than it display correct value but after if condition it didn't showing the alert message. I just want to compare the value of data to the value that is coming from login.php.

$("#login").click(function(event){

        event.preventDefault();
        var Email = $("#email").val();
        var Password = $("#password").val();
        $.ajax({
            url : "include/login.php",
            method : "POST",
            data : {CustomerLogin:1,CustomerEmail:Email,CustomerPassword:Password},
            success : function(data){
                if(data == 'true'){
                    alert(data);
                }
            }
        })
    })
  • 4
    Possible duplicate: [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323) – abhishekkannojia Jun 28 '17 at 13:08
  • What do you mean by "after if condition it didn't showing the value"? There is no code after the `if` block, so what would you *expect* to happen there? – David Jun 28 '17 at 13:08
  • Is the return data a boolean or string representation of a boolean? – Kramb Jun 28 '17 at 13:09
  • 1
    @Kramb that makes no sense.... all response data is sent as string – charlietfl Jun 28 '17 at 13:10
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ste-fu Jun 28 '17 at 13:10
  • display correct value? what is the value? do you mean you want if(data)? – Dalin Huang Jun 28 '17 at 13:11
  • @charlietfl He probably meant that he is not getting values outside the ajax call where he might have some statements depending on the returned value. – abhishekkannojia Jun 28 '17 at 13:13
  • @charlietfl I meant the initial value. – Kramb Jun 28 '17 at 13:16

1 Answers1

1

you cann't compare boolean with string value . So you need to pass the boolean value for comparison

if(data == true){
    alert(data);
}
            
OR
            
if(data){
    alert(data);
}
Vijay
  • 93
  • 1
  • 8
  • his problem is he is getting data in alert but as he put true value inside string quotes == is not working .. So it's not going inside if block . – Vijay Jun 28 '17 at 13:21
  • The alert is inside the if condition that means the condition evaluates to `true`. So your answer does not really solves the problem – abhishekkannojia Jun 28 '17 at 13:22
  • Do you think 'true' & true both are same ?? – Vijay Jun 28 '17 at 13:23
  • No they are not. But the point is, he said he is already getting the alert. – abhishekkannojia Jun 28 '17 at 13:24
  • let him check once ..it might useful for him – Vijay Jun 28 '17 at 13:25
  • even he mentioned .. you can see " but after if condition it didn't showing the value." – Vijay Jun 28 '17 at 13:25
  • that means before if .. he might use alert to see the value ..@a – Vijay Jun 28 '17 at 13:26
  • Maybe you are right but his question is poorly worded. Edit your answer a bit with some explanation. – abhishekkannojia Jun 28 '17 at 13:26
  • Data ***is a string***! – charlietfl Jun 28 '17 at 16:45
  • yes i have use alert before if condition then it is working but after if condition it is not working. I want to compare the data value to some specific value. After satisfying if condition next code will run otherwise not. – satish kumar Jun 29 '17 at 06:47
  • I want to compare data value with some specific string or integer value. – satish kumar Jun 29 '17 at 07:02
  • 1
    then you need to use specific logic for your requirements .. By this it will go inside if block . previously it was not going . I think you are doing ajax call incorrectly . With URL append username ,password & check this in postman or Advanced REST client . if you get acesstoken . then do ajax call set the credentials in localstorage . make another ajax call you may get value as required – Vijay Jun 29 '17 at 08:58
  • try if (data.CustomerLogin==1) – Vijay Jun 29 '17 at 10:10