I am fairly new to JavaScript and PHP, and it's the first time to encounter and use JSON. I think my AJAX request is fine, but even so, the $_POST
array is empty.
Here is the AJAX call:
$( "#submit" ).click( function() {
var submit_basic = $.post( 'index.php/submit_data/pass_basic_data',
get_experience_data()
);
submit_basic.done( function(data) {
alert(data); // for debugging purposes
});
});
And this is the function that takes the data from a table:
function get_experience_data() {
var temp, data_exp = [];
$( "#exptable tbody tr" ).each( function() {
temp = {
company: $(this).find('td').eq(0).html(),
position: $(this).find('td').eq(1).html(),
datestart: $(this).find('td').eq(2).html(),
dateend: $(this).find('td').eq(3).html(),
description: $(this).find('td').eq(4).html()
};
data_exp = data_exp.concat(temp);
});
return data_exp;
}
And for reference, the destination controller function that only prints the $_POST
array (by the way I am using CodeIgniter):
public function pass_basic_data() {
var_dump($_POST);
}
Can you please pinpoint the error I've made, since I can't find it. Thanks a lot!
UPDATE: I am getting this message in particular:
array(1) {
["undefined"] =>
string(0) ""
}
UPDATE:
Thanks for all the help guys! I already solved it. It made me dance all around the room.
I wrapped the return value in a {name : value}
pair.
$( "#submit" ).click( function() {
var post_vars = get_experience_data();
var submit_basic = $.post( 'index.php/submit_data/pass_basic_data',
{object: post_vars}
);
submit_basic.done( function(data) {
alert(data); // for debugging purposes
});
});