0

How can I merge a serialize date with an array with jquery for ajax post?

For instance,

var array = { name: "John", time: "2pm" };

$.post( "page.php", $("form").serialize() + array);

I get this,

Array
(
    [url] => another test
    [page_id] => 140
    [method] => update[object Object]
)

What can I do so I can merge them and get this as my result below?

Array
(
    [url] => another test
    [page_id] => 140
    [method] => update
    [name] => John
    [time] => 2pm
)
000
  • 26,951
  • 10
  • 71
  • 101
Run
  • 54,938
  • 169
  • 450
  • 748
  • use hidden fields in your form `name` and `time` and initialize them with values you want – Nil'z Sep 30 '13 at 14:59
  • See also Underscore.js's extend function: http://documentcloud.github.io/underscore/#extend – biziclop Sep 30 '13 at 15:00
  • Keep in mind that on the client side, your var `array` isn't an `array`, it's an `object`. You can simply loop through the properties of one object and add them to the second, see [this question](http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – Snuffleupagus Sep 30 '13 at 15:01

1 Answers1

1

Use $.param() to serialize an array/object

var array = { name: "John", time: "2pm" };

$.post( "page.php", $("form").serialize() + '&' + $.param(array));
secelite
  • 1,353
  • 1
  • 11
  • 19