Submit serialized multiple forms

Menangani http post untuk multiple form pada single page, menggunakan “serialize form”.

$("#btnSubmit").click(function (e) {
   e.preventDefault();
   var formString = "";
   $.each(arrayOfFormName, function (key, value) {
      formString += ("#" + value + ", "); // Membuat string dari kumpulan form, dipisahkan dengan tanda koma
   });
   formString = formString.substring(0, formString.length - 2); // Menghapus koma yang terakhir

   var form = $(formString).serialize(); // serialize form yang sudah digabungkan menjadi 1 string
   $.ajax({
      url: '@Url.Action(ActionSite.CreateMultiForm)',
      dataType: 'json',
      type: 'POST',
      cache: false,
      data: form,
      success: function (data) {
         setTimeout(function () {
            swal({
               title: "Success",
               text: data.Message,
               type: "success"
            }, function () {
               window.location.href = "@Url.Action(ActionSite.Index)";
            });
         }, 1500);
      },
      error: function (xhr, ajaxOptions, thrownError) {
         swal({
            title: "Error",
            text: "Internal server error",
            type: "error",
            timer: 1500
         });
      },
   });
});

Untuk serialize banyak form sekaligus, id form harus berupa format “form1, form2, …
Kemudian kirimkan AJAX dengan data post berupa multiple form yang sudah diserialize

Levanji Prahyudy has written 8 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>