/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Send Associated Array – Prototype AJAX Request

OK, my code here relies heavily on the Prototype library, but I will try and explain as best as I can. I am trying to cycle through some form elements, using prototypes CSS selectors. This returns an array. I am then looping through the array with the .each enumerable. Each time, I am taking the element, in this case it passes the object itself, and getting the name attributes and the value of the element. I then use the name as the key and the value of the element as the value of the array. The code thus far looks like this:

[code]
$$(‘#reportHeaderContent input’).each(function(el){

//detect type, so we can adjust accordingly
var inputType = el.getAttribute(‘type’);
var inputName = el.getAttribute(‘name’);
var inputValue = el.value;

//depending on type – we will populate array with proper information
if(inputType == ‘text’){

inputParameters[inputName] = inputValue;

}else if(inputType == ‘checkbox’ || inputType == ‘radio’){
if(el.checked == 1){

inputParameters[inputName] = inputValue;

}
}

});
[/code]

Everything, thus far, works. I can alert the array, although I do have to know the key ahead of time. That will be taken care of later, though, in PHP.

Now is the tricky part. I am going to send this via the Ajax.Updater object in Prototype. Code below:

[code]
new Ajax.Updater(‘mainReportContainer’, ‘/’+actualDomain+’/’+actualLocation, {method: ‘post’, evalScripts: true, parameters:{ajax: ‘ajax’, inputParams: inputParameters }});
[/code]

Don’t worry about the actual location and domain part. They work, too. Now, in my PHP I detect whether the ajax param is set and execute some arbitrary code based on it. The problem is that it’s not sending the second parameter, inputParams. It sends the ‘ajax’ param, but not the second.

Any suggestions?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@polygoneauthorAug 27.2008 — OK, I tried changing it from an array to an object and can now pass it. I alert the values and they seem to be ok. I am not sure how nicely PHP will play with it. I will update the status of this thread, as soon as I find whether this is an acceptable solution.
Copy linkTweet thisAlerts:
@toicontienAug 27.2008 — You need to do one of two things:

1) Serialize all your parameters into a string, and pass that string as the value for the "parameters" property.

2) Add the "ajax" property to the inputParameters variable:
[B]inputParameters["ajax"] = "ajax";[/B]
new Ajax.Updater('mainReportContainer', '/'+actualDomain+'/'+actualLocation, {method: 'post', evalScripts: true, [B]parameters:inputParameters[/B]});
Copy linkTweet thisAlerts:
@polygoneauthorAug 27.2008 — OK, I gotcha. I used the json.org method called 'stringify' and it works nicely. But for some reason, Prototypes .toJSON(); method won't work with objects. If I find out anything else on that, I'll post it.

Thanks for the input though, man.
Copy linkTweet thisAlerts:
@polygoneauthorAug 27.2008 — OK, my problem was with the way I was inplementing Prototypes .toJSON method.

Wrong:

varName.toJSON();

Right:

Object.toJSON(varName);
×

Success!

Help @polygone spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...