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); }
});
You can simply pass a JavaScript Array variable in the $. ajax as any other variable.
You cannot use the values returned from the AJAX request until that request has completed. You'll need to return a Promise[^], which will return the array value once the request has completed.
Create something like: var dataArgs = { checkedValues: checkedValues, action: action }; and then simply pass dataArgs object to data key as a value.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With