Aniello Caputo
Aniello Caputo

Reputation: 1

Call a jquery function after .submit()

I save the data on my DB. After i would display the new updated items calling the function "loadInitPosts()" This is my code:

function post_save_ajax() {
    $(this).submit();
    loadInitPosts();
}

function loadInitPosts() {
    $.ajax({
        type: "POST",
        url: "home/loadInitPosts",
        success: function (data) {
            $(".posts").empty();
            data = $.parseJSON(data);
            $(".posts").append(data);
        }
    });
}

The function loadInitPosts is correctly called, but display the old data. It seems that is called before submit execute his task.

Upvotes: 0

Views: 433

Answers (2)

StartCoding
StartCoding

Reputation: 393

function post_save_ajax() {
$(this).submit( function(){
loadInitPosts();
event.preventDefault();
});

}

 function loadInitPosts() {
$.ajax({
    type: "POST",
    url: "home/loadInitPosts",
    success: function (data) {
        $(".posts").empty();
        data = $.parseJSON(data);
        $(".posts").append(data);
    }
});
}

Upvotes: 0

martincarlin87
martincarlin87

Reputation: 11042

Try this:

$(this).submit(function () {
    loadInitPosts();
});

Upvotes: 1

Related Questions