Robert Martens
Robert Martens

Reputation: 173

How to clear an ajax form after submit?

the following is the ajax markup for processing my form:

$.ajax({
    dataType: "json",
    type: "get",
    url: "ajax.php?action=add_driver",
    data: $("#addDriver").serialize(),

    beforeSend: function(){
        $('.error, .success, .notice').remove();
    },

    success: function(json){
        if (json['status']=='success')
        {
            alert(json['message']);
            $("#addDriver").reset();
        }else{
            if(json['error']['my_variable']){
            $("input[name=my_variable]").after('<div class="error">'+json['error']['my_variable']+'</div>');    
            }
            $("#addDriver").reset();
        }
});

what I want to know is, how would i clear the form, once it has been submitted? I tired using $("#addDriver").reset(); but it just clears the form completely without submitting the form. Any help?

Upvotes: 0

Views: 10643

Answers (1)

Krule
Krule

Reputation: 6476

Well, it's not a question of how?, in your case, but of when?.

$("#addDriver")[0].reset(); should be just fine, provided that is id of your form, as long as you use it within success() or complete() callback function.

Upvotes: 2

Related Questions