简体   繁体   English

与$ _POST格式相同的Ajax多维表单数据

[英]Ajax multidimensional form data in same format as $_POST

I have a form with deep multidimensional inputs like so: 我有一个具有深层多维输入的表单,如下所示:

<form id="theForm">
 <input type='text' name='one[two][three][]' />
 <input type='text' name='one[two][three][]' />
 <input type='text' name='four[five][six][seven]' />
 <input type='text' name='eight[nine][ten]' />
</form>

I am trying to send the values via AJAX in the same format as if the form was submitted normally. 我试图通过AJAX以与正常提交表单相同的格式发送值。 What I mean is, if I were to hit the submit button, in PHP, $_POST would look like this. 我的意思是,如果要单击“提交”按钮,则在PHP中,$ _ POST看起来像这样。 This is what I want: 这就是我要的:

array( 'one' => array( 'two' => array('three'=>array( [0] => "", [1] => "" ) ) ),
       'four' => array( 'five' => array ('six' => array( 'seven' => "" ) ) ),
       'eight' => array( 'nine' => array( 'ten' => "" ) ) )
     )

I am essentially trying to AJAX the form submission. 我实质上是在尝试AJAX表单提交。 When I do 当我做

$("#theForm").serializeArray()

And the send that over $.post() my $_POST looks like this: 然后通过$.post()发送$ _POST,如下所示:

array( [0] => array( "name" => string "one[two][three][]",  value=>"")
       [1] => array( "name" => string "one[two][three][]",  value=>"")
       [2] => array( "name" => string "four[five][six][seven]",  value=>"")
       [3] => array( "name" => string "eight[nine][ten]",  value=>"")
     )

This is of no use to me. 这对我没有用。 I need to preserve the multidimensional structure I outlined before. 我需要保留之前概述的多维结构。 I suppose I could walk through the array and chop up the string to recreate what I need, but this seems asinine to me. 我想我可以遍历数组并切碎字符串以重新创建所需的内容,但这对我来说似乎是愚蠢的。 Is the a jQuery friendly way to accomplish this? 是实现此目标的jQuery友好方法吗?

I've read the other posts about how jQuery can send multidimensional arrays just fine, so maybe the question I need to ask is, how do I extract my form data, keeping it in a multidimensional array? 我已经阅读了其他有关jQuery如何发送多维数组的文章,所以也许我需要问的问题是,如何提取表单数据并将其保存在多维数组中?

My guess is when you are making the AJAX call you are doing: 我的猜测是当您进行AJAX调用时:

data: {data: $("#theForm").serializeArray()}

Don't do that, just set data to the value of serializeArray . 不要这样做,只需将data设置为serializeArray的值即可。

data: $("#theForm").serializeArray()

EDIT : You said you are using $.post , and I assume you are doing this: 编辑 :您说您正在使用$.post ,我认为您正在这样做:

$.post('/your/url', {data: $("#theForm").serializeArray()}, function(){});

In order for it to work, you need to do it this way: 为了使其正常工作,您需要采用以下方式:

$.post('/your/url', $("#theForm").serializeArray(), function(){});

Accepted answer is totally correct. 接受的答案是完全正确的。 As a side note, if you need to add other data to the array of post data, you just need to push it onto the array in the format {name: "", value: ""} 附带说明一下,如果您需要将其他数据添加到发布数据数组中,则只需将其以{name: "", value: ""}的格式推送到数组中

In my case, I wanted to add a value based on a button that the user clicked, in addition to the values from all of the inputs in the form. 就我而言,除了表单中所有输入的值之外,我还想基于用户单击的按钮添加一个值。 So all you have to do is: 因此,您要做的就是:

var data = $("#myForm").serializeArray();
data.push({name: "btnVal", value="button_a"});

And make sure you post like: 并确保您发布类似:

$.post("/some/url", data, function(){});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM