5

I have an array (for checkboxes) that I need to pass alongside the regular form in an ajax post, but can't seem to get this to work:

new_data = [a,b,c,d,e];

somedata_assoc = JQuery.param({'choices[]': new_data});

    $.ajax({
        type: "POST",
     url: contract_qurl,
     data: $(div).find("form").serialize()+"&"+somedata_assoc,
     context: $(this),
     success: function(data) { $("#results_table").html(data); }
    });
Dan
  • 63
  • 1
  • 1
  • 4
  • Are you getting errors? Have you tried using something like firebug to see what is happening with the ajax post? Maybe it would help if you included more code since the above won't run without errors. – Jason Webb Apr 22 '10 at 02:35

2 Answers2

7

After a reserach the only solution that worked for me was:

url='url/to/page'
choices = [1,2,3,4,5,6]
    $.post(url,{ 'choices[]': choices }, function(data){
          console.log(data);
    },'html');

Also, use 'choices' with brackets, so you will be able to retrive in the server in a single variable, otherwise it will be an post to each array element. That worked for me. You can see an other post here at stackoverflow.

I hope this will be able to help someone in the future.

Community
  • 1
  • 1
7

I'm getting a javascript error on this line

new_data = [a,b,c,d,e];

I had to change it to this

new_data = ['a','b','c','d','e']; 

you capitalized the J in jQuery in this line

somedata_assoc = JQuery.param({'choices[]': new_data});

should be this (or just the $ shorthand)

somedata_assoc = jQuery.param({'choices': new_data});

also, i dont think you need the brackets, in most cases they would make it more difficult to retrieve the data on the server

Yisroel
  • 8,164
  • 4
  • 26
  • 26
  • Thanks for so much for looking over this... I didn't realize the 'J' had to be lowercase! Also, you're right about the brackets. Everything posts fine now! – Dan Apr 22 '10 at 02:35