Omu
Omu

Reputation: 71188

send via $.post data + serialized form

I would like to do something like this (pseudo code):

<form id='f1'>
<input type='text' name='t1'/>
<input type='text' name='t2'/>
<input type='text' name='t3'/>
</form>

var ids=[9,32,45];
$.post(
"test.php", 
{$("#testform").serialize(), page: 7, ids: ids}, 
function(){ alert('success!'); }
);

at the server side I want to get the fields from the form + page and ids

is it possible ?

Upvotes: 3

Views: 6632

Answers (1)

Nick Craver
Nick Craver

Reputation: 630349

You need to create the array of values with .serializeArray() (where as .serialize() creates a string) then add to it before using as the data argument to $.post(), like this:

var data = $("#testform").serializeArray();
data.push({ name: "abc", value: "1" });
data.push({ name: "ef", value: "3" });
$.post("test.php", data, function(){ alert('success!'); });

Then you pass in the array, just like passing in an object, and then $.param() gets called internally, turning it into the data string for POST query.

Upvotes: 7

Related Questions