I am trying to send an array using the jQuery ajax function, but it isn't working.
Here is my code:
if (section_name == "first_details_div") {
var fields_arr = ["f_name", "l_name", "identity_number", "kupat_holim_id", "kupat_holim_insurance_ID", "birth_date", "father_name", "family_status_id"];
var section_values = new Array();
for (i = 0; i < fields_arr.length; i++) {
if (document.getElementById(fields_arr[i]))
section_values[fields_arr[i]] = document.getElementById(fields_arr[i]).value;
}
}
var array_to_send = $.serialize(section_values);
$.post("ajax_save_intek_section.php", {
section_name: section_name,
section_values: array_to_send
},
function(data) {
alert('here!');
if (data) {
alert(data); //"<?=getstring('saved_successfully')?>"
}
});
I tried to add this line before (as I saw in other answer):
var array_to_send = $.serialize(section_values);
but it doesn't recognize this function.
Any ideas?
Try to use var array_to_send= $(section_values).serialize();
stead. but you also can create an object and serialize it with JSON.sttringify(object);
var request = {
section_name: section_name,
section_values: section_values
};
$.post("ajax_save_intek_section.php", JSON.stringify(request),
function(data){
//rest of your code here
}
});
//Array of STRINGS
var fields_arr = ["f_name", "l_name", "identity_number", "kupat_holim_id",
//Array that takes INTEGER as an index
var section_values = new Array();
for (i = 0; i < fields_arr.length; i++) {
if (document.getElementById(fields_arr[i]))
//giving the STRING as an INDEX to the array instead of INTEGER
section_values[fields_arr[i]] = document.getElementById(fields_arr[i]).value;
}
}
you are doing something like
section_values["f_name"]=document.getElementById(fields_arr[i]).value;
which will keep your section_values as an empty array. I think you wanted to do this instead
section_values[i]=document.getElementById(fields_arr[i]).value;