php – jquery serializeArray表单中的JSON multiselect
作者:互联网
我有一个带有多选内部的表单.提交我有这个功能:
$( "#addTrainForm" ).submit(function( event ) {
var dataString = $(this).serializeArray();
console.log(dataString);
event.preventDefault();
$.ajax({
type: "POST",
url: "/index.php/trainings/insertTraining",
data: dataString,
dataType: 'json',
success: function(data) {
$("div#addModal").hide();
location.reload();
}
});
return false;
});
现在,您可以在其中看到控制台日志,我得到以下内容:
0: Object
name: "date"
value: "14/10/2013 16:22:46"
__proto__: Object
1: Object
name: "pool"
value: "1"
__proto__: Object
2: Object
name: "repeat_0"
value: "1"
__proto__: Object
3: Object
name: "distance_0"
value: "1"
__proto__: Object
4: Object
name: "style_0"
value: "1"
__proto__: Object
5: Object
name: "change_0"
value: "1"
__proto__: Object
6: Object
name: "time_0"
value: "1"
__proto__: Object
7: Object
name: "options_0"
value: "1"
__proto__: Object
8: Object
name: "options_0"
value: "2"
__proto__: Object
9: Object
name: "repeat_1"
value: "2"
__proto__: Object
10: Object
name: "distance_1"
value: "2"
__proto__: Object
11: Object
name: "style_1"
value: "1"
__proto__: Object
12: Object
name: "change_1"
value: "2"
__proto__: Object
13: Object
name: "time_1"
value: "2"
__proto__: Object
14: Object
name: "options_1"
value: "4"
__proto__: Object
15: Object
name: "options_1"
value: "5"
__proto__: Object
16: Object
name: "options_1"
value: "6"
您可以在其中看到options_0和options_1具有多个值.
现在在被调用的php函数中,我读取了所有的帖子数据并将其写入文件:
public function insertTraining(){
$data=$this->input->post();
$this->baselib->logIt(print_R($data,true));
}
在那里我的options_0和options_1只有最后一个值可用.我期待更多的值或一个逗号字符串或其他东西.这是我在文本文件上的登录情况:
Array
(
[date] => 14/10/2013 16:22:46
[pool] => 1
[repeat_0] => 1
[distance_0] => 1
[style_0] => 1
[change_0] => 1
[time_0] => 1
[options_0] => 2
[repeat_1] => 2
[distance_1] => 2
[style_1] => 1
[change_1] => 2
[time_1] => 2
[options_1] => 6
)
如何将options_0和options_1的所有值都添加到我的php脚本中?有没有更好的方法来使用我已经存在的函数(比如可选参数?)或者我是否必须运行循环arround所有值并找到选项值并将它们放入数组中然后使用json将其发送到PHP?
希望我设法解释自己,
谢谢,
詹姆士
解决方法:
找到了解决方案.问题既不是jquery也不是JSON,它只是在HTML中.你在哪里:
<select multiple id="..." name="foo">
你必须使用foo [],这将成为jquery方面的一个值数组,当你将它JSON到PHP时它会保持原样:)
所以上面的例子变成了:
<select multiple id="..." name="foo[]">
标签:jquery,json,php,forms,multi-select 来源: https://codeday.me/bug/20190624/1281904.html