Merge values from two forms on submit

前端 未结 9 921
甜味超标
甜味超标 2020-11-29 18:12

I have two forms on one html page. Using jQuery, is it possible to have the data from both forms go into the POST data when the first is submitted?

9条回答
  •  心在旅途
    2020-11-29 18:47

    This is a clean JavaScript approach for merging two forms. I test it with a POST request with Prototype and jQuery and it works. This is the thing:

    var data1 = document.getElementById('form1').serialize();
    

    NOTE: form1 is form's id .You need to set it within

    var data2 = document.getElementById('form2').serialize();
    

    NOTE: form2 is form's id. You need to set it within

    Now you have two vars and two serialized data (arrays). You can easily merge them. Your form will have assoc. array and you can get a problem when you try using concat function.

    var mergeddata = data1 + '&' + data2;
    

    This is it! You can easily send them through ajax call. For example, Prototype.js:

     new Ajax.Request(url, {
                            method: 'post',
                            parameters: mergeddata, ...
    

提交回复
热议问题