Jquery serializeArray for JSON multi selector in form

I have a form with 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 that you can see this 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"

Here you can see that options_0 and options_1 have several meanings.

Now in the php function called, I read all the message data and write it to a file:

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

there my options_0 and options_1 have only the last value. I was expecting a larger array of values ​​or a linked string or something else. Here is what my log looks like in a text file:

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 script? - (, ?) arround , PHP json?

, , ,

+4
1

. jQuery JSON, HTML. :

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

foo [], jquery, , JSON PHP:) , :

<select multiple id="..." name="foo[]">
+4

All Articles