jquery - Submit to 2 different PHP forms using Ajax -


i facing issues when want send 2 forms different values using 2 different php pages.

my ajax code this:

        $(document).ready(function() {           var form = $('#main_form_new');           var submit = $('.sbbtn');           var alert = $('.form_result');           form.on('submit', function(e) {             e.preventdefault();              $.ajax({               url: 'ajax/category.php',               type: 'post',               datatype: 'html',               data: form.serialize(),               beforesend: function() {                 alert.fadeout();                 submit.html('saving changes....');               },               success: function(data) {                 alert.html(data).fadein();                 form.trigger('reset'); // reset form                 submit.html('save changes');               },               error: function(e) {                 console.log(e)               }             });           });           }); 

for second form changed var , replaced as

          var itemform = $('#item_main_itemform_new');           var itemsubmit = $('.itemsbbtn');           var itemalert = $('.item_itemform_result');           itemform.on('submit', function(e) {             e.preventdefault();              $.ajax({               url: 'ajax/items.php',               type: 'post',               datatype: 'html',               data: itemform.serialize(),               beforesend: function() {                 itemalert.fadeout();                 itemsubmit.html('saving changes....');               },               success: function(data) {                 itemalert.html(data).fadein();                 itemform.trigger('reset'); // reset itemform                 itemsubmit.html('save changes');               },               error: function(e) {                 console.log(e)               }             });           }); 

this not work not know reason. doing wrong?

don't repeat yourself.

if need same functionality twice, don't copy , paste code. make function, use variables variable parts, call function twice.

$(function() {     function formsubmithandler(options) {         return function (e) {             var $form = $(this),                 $submit = $(options.submit),                 $alert = $(options.alert);              e.preventdefault();              $alert.fadeout();             $submit.html('saving changes...').prop({disabled: true});              $.post(options.url, $form.serialize())             .done(function (data) {                 $alert.html(data).fadein();                 $form.trigger('reset');             })             .fail(function (jqxhr, textstatus, errorthrown) {                 $alert.html(textstatus).fadein();                 console.log(arguments);             })             .always(function () {                 $submit.html('save changes').prop({disabled: false});             });         };     }      $('#main_form_new').submit(formsubmithandler({         url: 'ajax/category.php',         submit: '.sbbtn',         alert: '.form_result'     }));      $('#item_main_itemform_new').submit(formsubmithandler({         url: 'ajax/items.php',         submit: '.itemsbbtn',         alert: '.item_itemform_result'     })); }); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -