0

how can I set the variable dch to be the returned success ajax data?

var aid='11111111V';
var dch = 0;
$.ajax({
    type:"POST",
    url:"new_hpa_fun_aplcval.php",
    data:"aid="+aid,
    success: function(msg) {            
        if (msg =='OK'){
            dch=1;                    
        } else {
            dch=2;
        }
    }
});
if (dch==1){
     //same php code
}else if (dch==2){
     //another php code
}
AB Shaman
  • 21
  • 12

2 Answers2

1

Maybe you are unfamiliar with asynchronous operations. In your code, your if-else check at the bottom of your code is actually executing before your success callback.

What you are probably observing is that dch is always 0. You instead need to continue the execution of your code inside the callback:

var aid='11111111V';
var dch = 0;
$.ajax({
    type:"POST",
    url:"new_hpa_fun_aplcval.php",
    data:"aid="+aid,
    success: function(msg) {            
        if (msg =='OK'){
            // perform something here                    
        } else {
            // perform something here
        }
    }
});

In this case, you don't even need the dch variable.

Your other option is to make the AJAX call synchronous by adding async: false to the options in the $.ajax method. This will cause your code to block and wait for the response before it continues executing.

See the Wiki on Ajax for a more technical description of what's happening under the hood of jQuery.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
-1

You can try like this

1) Wrap the ajax within a function and return the value

2) What does "async: false" do in jQuery.ajax()?

function call() {
     var temp = 0;
     $.ajax({
        type:"POST",
        url:"new_hpa_fun_aplcval.php",
        async: false,  // Add this
        data:"aid="+aid,
        success: function(msg) {            
            if (msg =='OK'){
                temp = 1;                    
            } else {
                temp = 2;
            }
        }           
     });
     return temp;
    }        
var dch = call();
Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • `return` statements in asynchronous callbacks have no effect. This won't work. – Pointy Oct 12 '13 at 14:57
  • @Pointy Oh ok I didn't see it. So adding `ajax = false` will solve the problem right? please correct me if I meant wrong – Praveen Oct 12 '13 at 15:00
  • @user1671639: No. You would have to add `async: false`, and in addition, create a variable at the top of the function. In the `success` callback, you would set that variable, and finally, return the variable at the bottom (outside of the success callback). – Cᴏʀʏ Oct 12 '13 at 15:02
  • It could be made synchronous, but even then the `return` value from the callback will be completely ignored by jQuery. The "call" function has no `return` statement at all, so "dch" will always end up `undefined`. – Pointy Oct 12 '13 at 15:02
  • @Cory Yes I agree with you point of using variable to return values. But `return 1` and `return 2` will work right? I can't understand why it will not work. Please clarify this? – Praveen Oct 12 '13 at 15:06
  • @Pointy Thanks for the feedback. But I have the return statement, ` return 1; ` and ` return 2; ` This will work right. Please clarify it? – Praveen Oct 12 '13 at 15:08
  • 1
    **No it will not work.** Your `return` statements are **inside the callback function**, and the returned values will be ignored. – Pointy Oct 12 '13 at 15:11
  • @Pointy I got it. Just realized because of word "CALLBACK". Thanks for taking time to respond to comments. – Praveen Oct 12 '13 at 15:16