douduan5073 2013-10-14 14:32
浏览 176

jquery serializeArray表单中的JSON multiselect

I have a form with a multi-select inside. On submit I have this function:

$( "#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;
});

Now where you can see that console log I get the following:

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"

Where you can see options_0 and options_1 have multiple values.

Now in the php function that get's called I read all the post data and write it straght to file:

public function insertTraining(){
    $data=$this->input->post();
    $this->baselib->logIt(print_R($data,true));
}

in there my options_0 and options_1 only have the last value available. I was expecting more of an array of values or a commaseperated string or something. This is what my log on the text file looks like:

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
)

How can I get all the values for options_0 and options_1 to my php script? Is there some better way of using the functions I already am (like optional parameters?) or do i have to run a cycle arround all the values and find the options values and put them into an array and then send the to PHP with json?

Hope I managed to explain myself, Thank you, James

  • 写回答

1条回答 默认 最新

  • dqm74406 2013-10-14 15:26
    关注

    Found the solution. The problem is neither the jquery or the JSON, it's just in the HTML. Where you have:

    <select multiple id="..." name="foo">
    

    you have to use foo[] and that will become an array of values on the jquery side and it will stay like it when you JSON it to PHP :) So the above example becomes:

    <select multiple id="..." name="foo[]">
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献