Posting a multiple select using GetXmlHttpObject

R

reed.emmons

I have the following select box:
<select name="ddlState" multiple="multiple" size="5">

How would I post the array of values selected using the XMLHttpRequest/
XMLHTTP object? I have the following code:

xmlHttp=GetXmlHttpObject();
xmlHttp.open('POST',url,false);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xmlHttp.send('ddlState[]=' + document.addWine.ddlState)

One variable is easy but I have no idea how I would do the entire
array.

Thanks!
R
 
G

Gregor Kofler

(e-mail address removed) meinte:
I have the following select box:
<select name="ddlState" multiple="multiple" size="5">

How would I post the array of values selected using the XMLHttpRequest/
XMLHTTP object? I have the following code:

xmlHttp=GetXmlHttpObject();

GetXmlHttpObject()? What library is that?
xmlHttp.open('POST',url,false);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xmlHttp.send('ddlState[]=' + document.addWine.ddlState)

addWine? Anyway,...
One variable is easy but I have no idea how I would do the entire
array.

One possibility: JSON.
http://www.json.org/js.html

(You have to decode your json string on the server, too.)

Gregor
 
R

reed.emmons

(e-mail address removed) meinte:
I have the following select box:
<select name="ddlState" multiple="multiple" size="5">
How would I post the array of values selected using the XMLHttpRequest/
XMLHTTP object? I have the following code:
xmlHttp=GetXmlHttpObject();

GetXmlHttpObject()? What library is that?
xmlHttp.open('POST',url,false);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xmlHttp.send('ddlState[]=' + document.addWine.ddlState)

addWine? Anyway,...
One variable is easy but I have no idea how I would do the entire
array.

One possibility: JSON.http://www.json.org/js.html

(You have to decode your json string on the server, too.)

Gregor

--http://www.gregorkofler.at::: Landschafts- und Reisefotografiehttp://www..licht-blick.at ::: Forum für Multivisionsvorträgehttp://www.image2d.com::: Bildagentur für den alpinen Raum
The code for GetXmlHttpObject is as followed:

function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
 
R

RobG

I have the following select box:
<select name="ddlState" multiple="multiple" size="5">

How would I post the array of values selected using the XMLHttpRequest/
XMLHTTP object? I have the following code:

xmlHttp=GetXmlHttpObject();
xmlHttp.open('POST',url,false);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xmlHttp.send('ddlState[]=' + document.addWine.ddlState)

One variable is easy but I have no idea how I would do the entire
array.

Presumably you would format the values as if it was a form that was
submitted normally. That should not be too hard to work out: loop
over the select's options and see which ones are selected, encode
their values (or text, as appropriate) using encodeURLComponent and
send that, the format is:

?selectName=selectedOptionValue1&selectName=selectedOptionValue2


and so on. A simple example:

<script type="text/javascript">

function getSelectValue(sel) {
if (!sel.name) return;
var name = sel.name;
var opt;
var queryString = [];

if (sel.type == 'select-multiple'){

for (var i=0, len=sel.length; i<len; i++){
opt = sel.options;

if (opt.selected) {
queryString.push(name + '=' +
encodeURIComponent(getOptionValue(opt)));
}
}
} else {
opt = sel[sel.selectedIndex];
queryString.push(name + '=' +
encodeURIComponent(getOptionValue(opt)));
}
return queryString.join('&');
}

function getOptionValue(opt) {
return opt.value || opt.text;
}
</script>


<form action="">
<select multiple name="foo">
<option vlaue="one">one
<option vlaue="two">two
<option vlaue="three">three
</select>
<input type="button" onclick="alert(getSelectValue(this.form.foo));"
value="click me">
<input type="submit">
</form>


It is just an example, you will find better code for getting the value
of an option element here:

<URL:
http://groups.google.com.au/group/c...hl=en&lnk=gst&q=option+value#5df2df8147232cf7
 
P

Peter Michaux

[snip]
It is just an example, you will find better code for getting the value
of an option element here:

<URL:http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thr...

Finally I had the time to set up a subversion repository and trac site
to aggregate the results of sort of discussion. I've started by adding
the getOptionValue function.

<URL: http://cljs.michaux.ca/trac/browser/trunk/src/getOptionValue/>

The code that is in there is serious. The documentation and tests need
work.

We'll see how this goes :)

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top