Search code examples
jqueryajaxjquery-post

How can I pass full form + extra data via jquery $.post?


I send data with my form through jquery $.post to some PHP page, but now i want to add some extra data to this sent package. Here is my code:

    $.post("/settings/filter", $("#filter_form").serialize() , function(data,status,xhr)
    {
            my_data = data;

    }) 

I've tried to change it into

    $.post("/settings/filter", {$("#filter_form").serialize(), extra_variable:'extra1'} , function(data,status,xhr)

but it's wrong, how to do it?


Solution

  • You can use string concatination

    $("#filter_form").serialize() + '&extra_variable=extra1'
    

    It is because $("#filter_form").serialize() returns a string representation of the form like params1=x&params2=y, if the data is string then jQuery will not do any more processing on the data. so you need to do a string concatenation to append the extra values you need.