Tim H
Tim H

Reputation: 205

Popup wont open upon JQuery Ajax success

I can't get changeuserjudge.php to open. I've put it in several other places without any success whatsoever. Can anyone tell me why this is?

JQuery:

    $('#userCreateSave').click(function(){
                var success = false;    
                var createUserName = $('#createUserName').val();
                var createUserEmail = $('#createUserEmail').val();
                var createUserType = $('#createUserType').val();
                if (jQuery.trim($('#createUserEmail').val()).length<1){
                    alert ("Please enter an email address.");
                    return false;
                }
                else {

                    $.ajax({   
                            type: "POST",   
                            url: "adminmenu.php",   
                            data: 'createUserName=' + createUserName + '&createUserEmail=' + createUserEmail+ '&createUserType=' + createUserType,
                            success: function(){
                                success = true;
                                $('#newCreateRow').remove();
                                $('#tableUsers tr:first').after('<tr class="altr"><td>-</td><td>'+createUserName+'</td><td>'+createUserEmail+'</td><td>'+createUserType+'</td><td>Active</td></tr>');
                                $('#newUserLink').show();
                            }

                        }); 
                }
                    if(success) {
                       window.open("changeuserjudge.php"); 
                    }
            });     

Upvotes: 0

Views: 2412

Answers (4)

red-X
red-X

Reputation: 5128

the first a in ajax means asynchronous, meaning the success callback gets called out of the normal flow and thus after the if(success) statement. you should put the window.open("changeuserjudge.php"); inside the success callback.

Upvotes: 2

Genjuro
Genjuro

Reputation: 7735

you should include

window.open("changeuserjudge.php"); 

within the sucess callback function , also make sure to log to see wether the sucess function is executed or not

console.log("success")

the local variable success you created has nothing to do with the option sucess , it's always gonna be false .

Upvotes: 1

Evan
Evan

Reputation: 6115

why don't you put the open within your success function. thereby making sure to call it when the ajax call is completed. it won't be setting it to true because ajax is asynchronous

Upvotes: 2

Rob W
Rob W

Reputation: 348992

Instead of setting a boolean called success, create a function called success(), and execute it once the request has finished. Your code doesn't work as intended, because success is false immediately after the $.ajax() call.

Execution model:

.ajax()
success == false
AJAX finished: set success = true
End of function

A possible implementation:

var success = function(){
   window.open("changeuserjudge.php"); 
}
$.ajax({   
        type: "POST",   
        url: "adminmenu.php",   
        data: 'createUserName=' + createUserName + '&createUserEmail=' + createUserEmail+ '&createUserType=' + createUserType,
        success: function(){
            success(); //Execute the callback function: `success()`
            $('#newCreateRow').remove();
            $('#tableUsers tr:first').after('<tr class="altr"><td>-</td><td>'+createUserName+'</td><td>'+createUserEmail+'</td><td>'+createUserType+'</td><td>Active</td></tr>');
            $('#newUserLink').show();
        }

    }); 
}

Upvotes: 1

Related Questions