Question

This will most likely be a easy question for all you experts!

I have a associative array EX:

var ProfInfo= new Array();
ProfInfo['action'] = 'SaveNewProfile'; 
ProfInfo['other'] = 'moreStuff';
ProfInfo['oth'] = 'More Stuff';

and I need to send this array with a jQuery post. Currently I've tried the following:

$.post("ajax/ProfileMod.php", {
action:'SaveNewProfile',
data:ProfInfo}
, function(data, status) {
if(status == "success") {
// POST AJAX Script succesful
alert (data);

} else {
// POST AJAX Script error
alert ('AJAX POST error : Error saving New profile in ProfileMod.php');
}
});  // End AJAX POST Call

I can get the 'action' in the php $_POST, but not the associative array. So then I tried the following:

ProfInfo['action'] = 'SaveNewProfile'; // Add the action type in he array to pass
$.post("ajax/ProfileMod.php", ProfInfo
, function(data, status) {
if(status == "success") {
// POST AJAX Script succesful
    alert (data);
} else {
// POST AJAX Script error
    alert ('AJAX POST error : Error saving New profile in ProfileMod.php');
}
});  // End AJAX POST Call

And in this case I don't get anything at all. Where am I going wrong?

Was it helpful?

Solution

In JavaScript, new Array() instances can be abused and treated as associative arrays (aka objects), but you should construct ProfInfo like this instead:

var ProfInfo = {};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top