Juan
Juan

Reputation: 17

Jquery ajax - Make a function wait till the previous function gets executed

When the user clicks the Submit button, i am making a Jquery AJAX call to check if the user is an Admin User or not, if the User is a Admin i am showing a Information popup for the user with OK button.. Once the user clicks the OK button the form can be submitted.. If the user is not an Admin user then the form can be submitted.

Below is sample code

function btnFormSubmit()
{
   SubmitForm()
}

function SubmitForm()
{
IsUserAdmin();
SubmitForm();
}


function IsUserAdmin()
{
var userName = $('#txtUser').val();
           $.ajax({
                type: "GET",
                url: admin/checkisAdmin,
                dataType: "json",
                data: {
                "userId": userName
                },
             success: function (data) {
             if(data * 1){
              showAdminPopup();
             }
           else
           {
           }           
          },
error : function () {
}
});
}

function showAdminPopup() 
{
 var dlg = $("#divAdminDialog");
        dlg.dialog({                   
        width: 600,
        modal: true,
        create: function(e, ui) {

            },
        buttons: [{
             id: "ok_id",
             text: "OK",
             click: function() {
              $(this).dialog("close");
           }
          }]
        });
      }

The issue with the above is the IsUserAdmin() completes execution and shows the Popup and before the User clicks on OK button the SubmitForm() is executing and form is getting submitted

Upvotes: 0

Views: 57

Answers (0)

Related Questions