Helpful Information
 
 
Category: Ajax and Design
AJAX Post Strings PHP name arrays

How would I get the same functionality of this form



<form action="Pronunciation.php" method="POST">
<div><label><input type="checkbox" name="category[]" value="Medications" />Medications</label></div>
<div><label><input type="checkbox" name="category[]" value="Terminology" />Terminology</label></div>

<div><input type="text" name="t" />Search Text</div>
<input type="submit" value="submit" />
</form>


using ajax.

How would I write the post string in such a way that I can get an category array post to the php file.

I have



for(var i = 0; i < this.category.length; i++){
postStr += '&category%5B%5D=' + this.category[i];
}


so the post string ends up like.

?category%5B%5D=Terminology&category%5B%5D=Medications

but in the php file it says that category is not set.

Any ideas or alternatives to getting the array of categories to my php file using ajax?

Thanks,

Michael

Code ignored. Code deleted.

Ancora aka Mike H.,

STOP DOING THIS:

var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();

You are spreading very bad code. Please stop being fancy and use try catch. There are reasons why people are doing it that way! First IE7 uses Native Object. If ActiveX is disabled and you access the XMLHttp, it throws an error!

If you work in production environments with High Traffic and clientside error logging, you would know that is wrong in every way.

Eric

Sorry you feel that way. Not sure why you would have to respond in such a manner. I was just telling you that what you are doing will cause errors on people's machines that have ActiveX disabled.

I do research in this area and I know what problems area are. That is one of them.

Eric

That part is no big deal anyway. I mean I have a good (I think) requester code.



function getXMLHTTPRequest() {
var requester = false;
try {
requester = new XMLHttpRequest();
}
catch (error) {
var aVersions = [ "MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHTTP"];
for(var i = 0; i< aVersions.length;i++){
try {
requester = new ActiveXObject(aVersions[i]);
}
catch (error) {
continue;
}
}
}
return requester;
}


Where I have done some research also and hopefully have a good function going but what I would like is to build not only a post string but a post string that contains an array. Just like when you name a form element name="blah[]". If you do that to multiple elements the post will contain an array called blah. I would like to write a post string that builds an array like that for me. Is it possible?

Thanks for responding.

Code ignored. Code deleted.

OOPS!

I forgot to set the request header

oHTTPPronounceReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

that made it work like I wanted.

blah[]=blah1&blah[]=blah2

:)

Sorry.










privacy (GDPR)