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
    });
}
11
  • print what response? Commented Oct 6, 2017 at 18:21
  • Like an alert success message Commented Oct 6, 2017 at 18:23
  • you'll have to wait until the success message exists. you haven't provided code that even attempts to print the message, so... we have no idea what you are actually doing wrong, all we can do is guess. Commented Oct 6, 2017 at 18:23
  • basically the alert message is executing before post function Commented Oct 6, 2017 at 18:24
  • well, no, it's executing after the post function, but before the post request has completed. Commented Oct 6, 2017 at 18:24

1 Answer 1

-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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.