0

I am writing a code in which user checks multiple check boxes and those users assign to the selected division. I am using $.post function in a loop of check boxes. Everything is working fine and checked users are being assigned to the selected division. Now, the issue is that I want to print a response after $.post function but everything I write after this post function, javascript executes it before that function and that's why, I am unable to print the response. Please check the below code for your convenience and suggest a possible solution.

Thanks a lot

function assign_user(div_id){
    //alert(div_id);
    var favorite = [];
    $.each($("input[name='user']:checked"), function(){
        value = $(this).val();
        $.post("../db/assign_user.php",{div_id:div_id,value:value}, 
        function(x){

        });
// I want to print final response here but it executes before post function
    });
}
Talal Haider
  • 15
  • 1
  • 4

1 Answers1

-1

Ajax requests are asynchronous. They are sent to the server, then the javascript continues on without waiting for a response. If you type your Ajax request like:

$.ajax({
  url: "../db/assign_user.php",
  type: 'POST', 
  data: {div_id:div_id,value:value}, 
  success: function(data){ 
    alert(data); 
  }
});

then in the success function, you can handle or alert the response.

Niro
  • 776
  • 8
  • 15