javascript - generic ajax form submission -
ajax/javascript problem: have app consist of multiple forms. want achieve make generic js function submit forms respective controllers getting form id.. m getting form ids in form_id variable m unable use them. tried replacing $('patient_form') form _id , got following error: typeerror: form_id.on not function
here following code better understanding of problem:
$(function () { var form = document.getelementsbytagname("form"); var form_id = "'#" + form[0].id + "'"; form_id.on('submit', function (e) { e.preventdefault(); $.ajax({ type: 'post', url: 'controllers/c_insertpatient.php', data: $('#patient_form').serialize(), success: function (result) { alert(result); } }); }); });
in addition other answers, want keep form id dynamic, right, can insert whatever values want?
$(function () { var form = document.getelementsbytagname("form"); // note have convert jquery object var form_id = $("#" + form[i].id); // i, can put in id form form_id.on('submit', function (e) { e.preventdefault(); $.ajax({ type: 'post', url: 'controllers/c_insertpatient.php', data: $(this).serialize(), // again, keep generic applies form success: function (result) { alert(result); } }); }); });
Comments
Post a Comment