2

I'm trying to pass an array trough an Ajax POST request using jQuery. I can't seem to get it done. Here is my code:

var settings = [];
$('.settingp input').each(function(){
    settings[$(this).attr('id')] = $(this).val();
});
$.post("editSettings.php", { 'settings': settings });

The request happens, but there is no data in it. Any idea what am I doing wrong?

trejder
  • 17,148
  • 27
  • 124
  • 216
Mars
  • 4,197
  • 11
  • 39
  • 63
  • no I checked and the array is being set up correctly. settingp is

    :p
    – Mars Dec 25 '10 at 19:03
  • settings.length seems to be returning 0 even when settings['key'] is returning the value – Mars Dec 25 '10 at 19:11
  • This question was asked recently. Please check : [**http://stackoverflow.com/questions/4402036/jquery-ajax-posting-array-to-asp-net-mvc-controller**](http://stackoverflow.com/questions/4402036/jquery-ajax-posting-array-to-asp-net-mvc-controller) – Chandu Dec 25 '10 at 17:23
  • tried the solution from that question but still no data is being passed – Mars Dec 25 '10 at 17:31
  • Can you use a Firebug to see what does the post look like? – Chandu Dec 25 '10 at 17:36
  • i would alert the array before passing it... – Uku Loskit Dec 25 '10 at 17:39
  • hmm.. I changed var settings to window.settings and checked the DOM using firebug to see if the array was being set filled right and it was all ok but still no data. the post tab for the request in firebug is empty – Mars Dec 25 '10 at 19:07

2 Answers2

3

the problem was

$(this).attr('id') => retuns a string, not a number

settings[$(this).attr('id')] = $(this).val();

changed it to and now it works

settings[settings.length] = [$(this).attr('id'), $(this).val()];

thanks everybody for trying to help me

Mars
  • 4,197
  • 11
  • 39
  • 63
2

What about this?

var settings = {};
$('.settingp input').each(function(){
    settings[$(this).attr('id')] = $(this).val();
});
$.post("editSettings.php", settings);

I think the issue is with our settings is initialized. Then when posting it, you don't have to call it "settings", since you have set all of the names of the post values already.

Travis D
  • 318
  • 1
  • 2
  • 10