Nathan
Nathan

Reputation: 509

combining ajax code for multiple forms

i have the 2 scripts below that pretty much do the same thing however are for 2 different sets of forms but get posted to the same page via ajax my question is there a way to minimize the amount of code required to do the job

like for example $(document).on('submit', 'form.frm_details','form.wa_cu_del', function(event)

<script>
$(function() {
    $(document).on('submit', 'form.frm_details', function(event) {
        event.preventDefault();
        $.ajax({
            url: '/limitless/functions2.php',
            type: 'post',
            dataType: 'json',                       
            data: $(this).serialize(),
            success: function(data) {
                if(data.status == '1') {
                    $('#info').html('<div class=\"alert alert-success alert-styled-left alert-arrow-left alert-bordered\"> ' + data.message + '<button type=\"button\" class=\"close\" data-dismiss=\"alert\" >×</button></div>');
                    $('.my-modals').modal('hide');
                    webapp_get_customers(); 
                    document.getElementById('frm_details0').reset();
                } 
                if(data.status == '2') {
                    $('#info').html('<div class=\"alert alert-danger alert-styled-left alert-arrow-left alert-bordered\">' + data.message + '<button type=\"button\" class=\"close\" data-dismiss=\"alert\" >×</button></div>');
                    $('.my-modals').modal('hide');
                }                               
            }  
        });
    });
});
</script>

<script>
$(function() {
    $(document).on('submit', 'form.wa_cu_del', function(event) {
        event.preventDefault();
        $.ajax({
            url: '/limitless/functions2.php',
            type: 'post',
            dataType: 'json',                       
            data: $(this).serialize(),
            success: function(data) {
                if(data.status == '2') {
                    $('.my-modals').modal('hide');
                    $('#info').html('<div class=\"alert alert-danger alert-styled-left alert-arrow-left alert-bordered\"> ' + data.message + '<button type=\"button\" class=\"close\" data-dismiss=\"alert\" >×</button></div>');
                    webapp_get_customers(); 
                }                               
            }  
        });
    });
});
</script>

Upvotes: 0

Views: 37

Answers (1)

KLin
KLin

Reputation: 479

There is a change in your code, instead of

$(document).on('submit', 'form.frm_details','form.wa_cu_del', function(event)

you had to write

$(document).on('submit', 'form.frm_details, form.wa_cu_del', function(event)

Upvotes: 2

Related Questions