I have a form that contains fields of an associative array:
<label for="my_array[][system]">System</label>
<input id="my_array[][system]" type="text" value="" name="my_array[0][system]">
<label for="my_array[][name]">Name</label>
<input id="my_array[][name]" type="text" value="" name="my_array[0][name]">
I'm trying to get this form posted to PHP using Ajax. I've attempted this:
$.ajax({
type: "POST",
dataType: "json",
url: ajaxurl,
data: {
action: "update_postmeta",
post_id: post_id,
nonce: nonce,
my_array: $('input:text[name="my_array*"]')
.each(function() { values[this.name] = $(this).val() })
},
success: function (response) {
alert(response);
},
error: function ( jqXHR, textStatus, errorThrown) {
alert("Error: " + textStatus + '\r\n\r\n' + errorThrown);
}
})
The problem is on this line of code:
my_array: $('input:text[name="my_array*"]')
.each(function() { values[this.name] = $(this).val() })
.each() is not a function... I'm not sure how to get my_array populated with the form's data in the same structure it would be using a regular form submission (without Ajax).
How do I post a form's data when it is created with an associative array?
$.fn.each()is a function, and it should work to populate an array. Are you saying.each()isn't working, or that you can't post the array as data via$.ajax()?.ajax()API. The function signature places url as the first parameter:jQuery.ajax( url [, settings ] )