Raunak Agarwal
Raunak Agarwal

Reputation: 7228

Jquery: How to confirm form submission via ajax

I have an application running on Django and Jquery. I am submitting a Bootstrap Modal form via ajax. I do server side form validation if found any errors I return the form. Otherwise, I return an html table with the content of the form which I would like to display. But how can I know in the front end the server returned html content is a form or the result table?

$('#id_form').live("submit", function(e){
    e.preventDefault();
    form = $(this);
    frequest_id= form.attr('data-frequest-id');
    submitForm('#myModal', form, frequest_id);
});


function submitForm(target, form, frequest_id){
    $.ajax({
        url:form.attr('action'),
        type:'post',
        data:form.serialize(),
        dataType:"html",
        error:function(data, status, xhr){
            Error();
            LoadingDone();
        },
        beforeSend:function(){
            Loading();
        },
        success:function(data, status, xhr){
            $(target).modal(data);
                    //if form has submitted call method to add result to the div
        },
        complete:function(){
            LoadingDone();
        }
    });
}

Upvotes: 0

Views: 504

Answers (1)

ace
ace

Reputation: 7583

You can use jQuery's .is() method.

success:function(data, status, xhr){
    $(target).modal(data);
    if ($(data).is('form')) {
        //if form has submitted call method to add result to the div
    }
}

Upvotes: 2

Related Questions