0

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

1
  • 1
    Try this: $thistabdata = {}; Commented Aug 29, 2016 at 22:23

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! 3 hours over 4 brackets!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.