Reputation: 10348
I have the following code:
var social_buttons_array = [];
social_buttons_array["google"] = $("input[name='social_popup_google']").is(":checked") ? 1 : 0;
social_buttons_array["twitter"] = $("input[name='social_popup_twitter']").is(":checked") ? 1 : 0;
social_buttons_array["twitter_follow"] = $("input[name='social_popup_twitter_follow']").is(":checked") ? 1 : 0;
social_buttons_array["facebook"] = $("input[name='social_popup_facebook']").is(":checked") ? 1 : 0;
And I am trying to pass the array like this:
$.get(
ajaxurl,
{
action: 'process_data',
get: 'social_popup',
social_buttons_array : social_buttons_array // not works
},
function(response) {
},
'json'
);
This do not works. Any idea to pass the array?
EDIT && solution
I edit this question to replace the associative array
by an object that will act as an array.
var social_buttons_array = new Object();
social_buttons_array.google = $("input[name='social_popup_google']").is(":checked") ? 1 : 0;
social_buttons_array.twitter = $("input[name='social_popup_twitter']").is(":checked") ? 1 : 0;
social_buttons_array.twitter_follow = $("input[name='social_popup_twitter_follow']").is(":checked") ? 1 : 0;
social_buttons_array.facebook = $("input[name='social_popup_facebook']").is(":checked") ? 1 : 0;
$.get(
ajaxurl,
{
action: 'process_data',
get: 'social_popup',
social_buttons_array : JSON.stringify(social_buttons_array) // it works great over an object
},
function(response) {
},
'json'
);
To manage this array/object in php we need:
$social_buttons_array = json_decode(stripslashes($_GET['social_buttons_array']));
And then we must manage this var as an object:
echo $social_buttons_array->google
// results in 1 or 0
Upvotes: 0
Views: 2817
Reputation: 19743
A GET request places your values in the URL in the form:
page.php?arg1=value&arg2=value2
So you cannot just pass an associative array unless you somehow convert it to a string value (maybe in Json format, as antisanity suggested).
Another option may be to pass each key of the dictionary as a URL parameter.
var urlParams = {
action: 'process_data',
get: 'social_popup',
};
for (key in social_buttons_array) {
urlParams[key] = social_buttons_array[key];
}
$.get(ajaxurl, urlParams, function(data) {
$('.result').html(data);
});
Which will send something like this:
page.php?action=process_data&get=social_popup&google=0&twitter=0&twitter_follow=0&facebook=0
It really depends how you are going to process that data on the server side.
Upvotes: 1
Reputation: 41715
Serialize it with JSON.stringify()?
social_buttons_array : JSON.stringify(social_buttons_array)
Upvotes: 3