jQuery。序列化()的后? [英] jQuery .serialize() for post?
问题描述
ajax请求的数据看起来像
data:{
somevar1:'someval1',
somevar2:'someval2',
somevar3:'someval3',
somevar4:'someval4',
form:FORMDATA
}
可以看到,以及基本表单数据,我也通过一些其他数据。
这意味着我不能使用jquery .serialize( )
我想结束一些事情,我可以通过另一边发送我可以做的事情。 b
$ b $ p $ $ POST ['form'] ['fieldname']
有没有一个内置函数做到这一点?
或者我需要做什么来创建一个?
在表单上运行函数的可能性,如
函数postArray(form){
var da ta = {};
var name,value = null;
$(form).children('textarea,input,select'){
name = this.name;
data.name = $(this).val();
}
返回数据;
}
可以工作。
并且有
data:{
somevar1:'someval1',
somevar2: 'someval2',
somevar3:'someval3',
somevar4:'someval4',
form:postArray(form)
}
会起作用吗?
我可以使用.serializeArray();
但是另一方面,我得到Array
(
[0] => Array
(
[name] => name1
[value] => val1
) ,
[1] => Array
(
[name] => name2
[value] => val2
)
.. 。
[8] =>数组
(
[name] => name8
[value] => val8
)
)
哪个关闭,但这要求我在数组中循环
a:并将其转换为我想要的
b:每次我使用它循环它以找到正确的键
解决方案您可以使用
serializeArray
和JSON.stringify
。
serializeArray
给你一个这样的数组:
[
{
名称:a
值:1
},
{
名称:b
值:2
}
]
所以你会不能访问服务器上的数据与
$ data ['fieldname']
一起。要将数组转换为对象(以便您可以像访问数据那样访问数据),请查看使用jQuery将表单数据转换为JavaScript对象。
当然,您也可以使用自己的函数(这似乎做了类似的事情如果你修正错误‡),但你仍然必须使用
JSON.stringify
:data:{
somevar1:'someval1',
somevar2:'someval2',
somevar3:'someval3',
somevar4 :'someval4',
form:JSON.stringify(postArray(form))
}
在你的PHP脚本中,你可以使用
json_decode
来获取表单数据:$ form_data = json_decode($ _ POST ['形成']);
‡这是一个固定版本注释,请勿在中使用
来循环数组):
function postArray(form){
var data = {};
form = $(form).serializeArray();
for(var i = form.length; i--;){
data [form [i] .name] = form [i] .value;
}
返回数据;
$ / code>如果您想为
[ ]
,您必须手动处理这些元素并将它们放入数组中:
for(var i = form.length; i--;)
var name = form [i] .name;
var value = form [i] .value;
var index = name.indexOf(`[]`);
if(index> -1){
name = name.substring(0,index);
if(!(name in data){
data [name] = [];
}
data [name] .push(value);
}
else {
data [name] = value;
}
}
但假设您没有字段名称,例如
字段
和字段[]
。
I am doing an ajax request through post that contains a forms variables.
the data for the ajax request looks like
data : { somevar1: 'someval1', somevar2: 'someval2', somevar3: 'someval3', somevar4: 'someval4', form: FORMDATA }
as you can see, as well as the basic form data I am also passing through some other data.
this means that I cannot use jquery
.serialize()
I am wanting to end up with something that I can send through so on the other side I can just do
$_POST['form']['fieldname']
is there a built in function do do this?
or what would I need to do to create one?
the possibility of running a function on the form that does something like
function postArray(form){ var data = {}; var name, value = null; $(form).children('textarea, input, select'){ name = this.name; data.name = $(this).val(); } return data; }
over the form could work.
and having
data : { somevar1: 'someval1', somevar2: 'someval2', somevar3: 'someval3', somevar4: 'someval4', form: postArray(form) }
would it work?
I could use.serializeArray();
But on the other side I getArray ( [0] => Array ( [name] => name1 [value] => val1 ), [1] => Array ( [name] => name2 [value] => val2 ) ... [8] => Array ( [name] => name8 [value] => val8 ) )
Which is close, but that requires me to either
a: loop over the array and convert it to what I want
b: every time i use it loop over it to find the right key解决方案You can encode your form data as JSON using
serializeArray
andJSON.stringify
.
serializeArray
gives you an array like so:[ { name: a value: 1 }, { name: b value: 2 } ]
so you would not be able to access the data on the server side with
$data['fieldname']
. To convert the array to an object (so that you could access the data like you want to), have a look at Convert form data to JavaScript object with jQuery.Of course you can also just use your own function (which seems to do something similar if you fix the errors‡), but you still have to use
JSON.stringify
:data : { somevar1: 'someval1', somevar2: 'someval2', somevar3: 'someval3', somevar4: 'someval4', form: JSON.stringify(postArray(form)) }
In your PHP script, you can then use
json_decode
to get the form data:$form_data = json_decode($_POST['form']);
‡ Here is a fixed version (updated from you comment, don't use
for...in
to loop over arrays):function postArray(form){ var data = {}; form = $(form).serializeArray(); for(var i = form.length; i--; ) { data[form[i].name] = form[i].value; } return data; }
If you want to fix it for elements with
[]
in the name, you have to process those elements manually and put them in an array:for(var i = form.length; i--; ) var name = form[i].name; var value = form[i].value; var index = name.indexOf(`[]`); if( index > -1) { name = name.substring(0,index); if(!(name in data) { data[name] = []; } data[name].push(value); } else { data[name] = value; } }
but this assumes that you don't have field names like
field
andfield[]
.这篇关于jQuery。序列化()的后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!