使用jquery ajax在单个提交中提交2个表单数据

问题描述 投票:0回答:2
javascript jquery ajax
2个回答
1
投票

您可以连接两个表单数据,如下所示:

  $.ajax({
    url: '/submit_form_data.php',
    type: 'POST',
    data: $("#form1").serialize() + '&' + $("#form2").serialize() ,

    success: function(response) {
      // Handle the response from the server.
    }
  });


0
投票

我得到的工作解决方案为:

$(document).ready(function() {
  // Create a FormData object for each form.
  var formData1 = new FormData($('#form1')[0]);
  var formData2 = new FormData($('#form2')[0]);

  // Append the FormData objects to a single object.
  var formData = new FormData();
  formData.append('formData1', formData1);
  formData.append('formData2', formData2);

  // Make an Ajax request to the server, passing in the FormData object as the data parameter.
  $.ajax({
    url: '/submit_form_data.php',
    type: 'POST',
    data: formData,
    processData: false, // Don't process the data as a query string.
    contentType: false, // Don't set the Content-Type header.
    success: function(response) {
      // Handle the response from the server.
    }
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.