How to pass additional data with form serialized data to ajax?

How can I pass additional data using the post ajax data serialization form ?.

below is my code which was used for the ajax post,

   $(document).ready(function()
     {
       var additional_data=$("#extra_data").val();
        $.ajax({
        type: 'POST',
        url: 'send_mail.php',
        data: frm.serialize(),
        success: function (data) {
            alert(data);
        }
    });
    });

      

here's how to pass additional_data

with form dataserialize

+3


source to share


4 answers


From jQuery API DOCS

The .serializeArray() method creates a JavaScript array of objects

The .serialize() method creates a text string in standard URL-encoded notation.

      

I think to use push

, we need to useserializeArray



try using

var frmData = frm.serializeArray();
frmData.push({name: "name", value: "test"});


$(document).ready(function()
     {
       var additional_data=$("#extra_data").val();
        $.ajax({
        type: 'POST',
        url: 'send_mail.php',
        data: frmData,
        success: function (data) {
            alert(data);
        }
    });
    });

      

+1


source


You need to push items to existing serialized data.



var frmData = frm.serialize();
frmData.push({name: nameofthevariable, value: valueofthevariable});
frmData.push({name: nameofthevariable2, value: valueofthevariable2});
frmData.push({name: nameofthevariable3, value: valueofthevariable3});
$(document).ready(function()
     {
       var additional_data=$("#extra_data").val();
        $.ajax({
        type: 'POST',
        url: 'send_mail.php',
        data: frmData,
        success: function (data) {
            alert(data);
        }
    });
    });

      

+1


source


serialize()

create a form query string. This way you can add additional parameters to it.

$(document).ready(function()
     {
       var additional_data=$("#extra_data").val();
        $.ajax({
        type: 'POST',
        url: 'send_mail.php',
        data: frm.serialize()+'&param1='+value1+'&param2='+value2,
        success: function (data) {
            alert(data);
        }
    });
    });

      

0


source


serializearray () can be used to send additional parameters. PFB code to send additional parameters.

var request = $('form').serializeArray();
request.push({name: "kindOf", value: "save"}); 

      

Ajax call

$.ajax({

                    url: "/ST/SubmitRequest",
                    dataType: "json",
                    //contentType: "application/json",
                    type: "POST",
                    data: request,
                    //data: r1,
                    success: function (response) {
                        //Setinterval();
                        //alert("Done...!");
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                }); 

      

0


source







All Articles