Send via $ .post data + serialized form

I would like to do something like this (pseudocode):

<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!'); }
);

server side. I want to get fields from the form + page and identifiers

Is it possible?

+3
source share
1 answer

You need to create an array of values ​​with .serializeArray()(where .serialize()creates the string), then add to it before using as an argument data $.post(), for example 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 an array, like passing an object, and then it $.param()is called internally, turning it into a data string for a POST request.

+7
source

Source: https://habr.com/ru/post/1783654/


All Articles