McGreeb
McGreeb

Reputation: 13

jQuery Post not posting multidimentional array data

I have a variable amount of tabs with custom forms on them I am trying to post the data from.

my solution was to make an array containing another array for each tab. then post that multidimensional array to my controller.

$("#savetabs").click(function()
{
    $("body").addClass("loading");
    var $alltabdata = [];
    $("#customtabs-holder").children().each(function() {
        $tablename = $(this).attr('id');
        $thistabdata = [];

        $inputs = $(this).find("input");
        $.each($inputs, function( index, value ) {
            $thistabdata[$(this).attr("name")] = $(this).val();
        });

        $selects = $(this).find("select");
        $.each($selects, function( index, value ) {
            $thistabdata[$(this).attr("name")] = $(this).val();
        });
        $alltabdata[$tablename] = $thistabdata;
    });
    console.log($alltabdata);

    posting = PostTabData($client_id, $alltabdata);

    posting.done(function( data ) {
        $("body").removeClass("loading");
        console.log(data);
        alert(data);
    });

    posting.fail(function() {
        $("body").removeClass("loading");
        alert( "Failed to post tab info" );
    });
});

function PostTabData($client_id, $tabdata) {
    console.log($tabdata);
    debugger;
    return $.post("/CustomTables/Contents/Save",
    {
        client_id: $client_id,
        alltabdata: $tabdata,
    });
}

the console.log before the post displays the correct info, But the tab data doesn't end up in my controller and when i check chromes developer tools under the network option it shows the sent data as only consisting of the client_id field

Upvotes: 0

Views: 46

Answers (1)

Maciej Sikora
Maciej Sikora

Reputation: 20132

Change your variables to objects:

from [] to {}

In javascript exist only Array with numeric keys. Associative Arrays not exists in javascript.

Check this snippet to fill the difference:

//array
var arr=[];
arr["name"]="Example name";

console.log("Here array log:");
console.log(arr);

//object
var obj={};
obj["name"]="Example name";

console.log("Here object log:");
console.log(obj);

Upvotes: 1

Related Questions