Sanjay Yadav Sanjay Yadav - 1 month ago 15
Ajax Question

How to combined two form data by jquery object

I have two forms with data and I am trying to merge this data for sending to my Api. Single form data is sending but I have no idea how to merge the data from both forms and send to my api.

$(document).ready(function () {
$("#place_order").click(function () {
var person2 = new Object();
person2.Title = $('#Title').val();
person2.FirstName = $('#FirstName').val();
person2.LastName = $('#LastName').val();

debugger;
/* #check1 is form id , for first form , please let me know how two use second form id #check2 */
var person = $('#check1').serialize();

$.ajax({
url: 'http://192.168.1.102:1512/qlikapi/RegisterUser',
type: 'Post',
data: person,
success: function(data, textStatus, xhr) {
alert(data.ErrorMessage);
if (data.Success) {
document.location.reload();
}
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});
});

Answer
$(document).ready(function () {
    $("#place_order").click(function () {
        var person2 = new Object();
        person2.Title = $('#Title').val();
        person2.FirstName = $('#FirstName').val();
        person2.LastName = $('#LastName').val();

        debugger;
        /* #check1 is form id , for first form , please let me know how two use second form id #check2 */
        var person = {};
        var person1 = {};

         $.map($('#check1').serializeArray(), function(n, i){
           person[n['name']] = n['value'];
         });

         $.map($('#check2').serializeArray(), function(n, i){
            person1[n['name']] = n['value'];
         });

        var mergedFormObj = $.extend({},person,person1);

        $.ajax({
            url: 'http://192.168.1.102:1512/qlikapi/RegisterUser',
            type: 'Post',
            data: mergedFormObj,
            success: function(data, textStatus, xhr) {
                alert(data.ErrorMessage);
                if (data.Success) {
                    document.location.reload();  
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                console.log('Error in Operation');
            }
        });
    });
});    

Make sure both object properties are unique.

Comments