Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XMLHttpRequest form submission with multiple select box 2

Status
Not open for further replies.

travisbrown

Technical User
Joined
Dec 31, 2001
Messages
1,016
Okay, I haven't been able to find any helpful reference to this on the Google yet, but I'm sure someone must have a simple solutions.

I need to post or get a form using XMLHttpRequest. The form has a multiple select box. How do you group all the values into either select=a,b,c or select=a&select=b&select=c? Simply calling the values like I would other form items only seems to call the first selected index.

Or... is there a way to automatically collate all the name/value pairs of a form into a querystring-like variable for submission?
 
When you read the select box value from the page you submitted to it should already have those values as a single string with commas as delimiters. At least this is how I have retrieved them in ASP.
If you have a multi select field named MyField then you would retrieve the values in ASP like this:

Method GET
Total = Request.QueryString("MyField").Count
For i = 1 to Total
Response.Write Request.QueryString("MyField")(i) & "<br>"
Next

Method POST
For Each item In Request.Form("MyField")
Response.Write item & "<br>"
Next


If I misunderstood your question I apologize.
I am curious how you used XMLHTTPRequest to submit the form though. Do you mean the screen remains on the same page but the form submission still occurs in the background?


Google, you're my hero!
 
No, I'm not having a problem retrieving the value on the target page, but using something like below only adds the first selected index. It's apparently not like a standard html form submission that automatically creates and array of values or multiple name/value pairs.

call_available_resources.asp?selCategory=' + document.forms['frmFilter'].selCategory.value + '...

I guess I need to loop through the selected indices? What's the best method?

 
what you will need to do is loop through each element in the select and test the "selected" property. something like this:

Code:
var s;
var o = document.forms['frmFilter'].elements['selCategory'].options;
for ( var i = 0; i < o.length; i++ ) {
    if ( o[i].selected ) s += (s.length > 0) ? (", " + s.value) : s.value;
}
alert (s);

i haven't tested that, but it should work.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I see, so it never is truly submitted, you build the string to pass.

I would give the select box an ID tag and do it like this:
Code:
function setURL() {
  var myURL = 'call_available_resources.asp?';
  var mySel = document.getElementById('MyField');
  var firstpass = true;
  for (var x=0; x<mySel.options.length; x++) {
    if (mySel.options[x].selected) {
      if (firstpass) 
        firstpass = false;
      else 
        myURL += "&";
      myURL += 'selCategory=' + mySel.options[x].value;
    }
  }
}

Google, you're my hero!
 
cLFlaVA's code takes a bit sleeker approach than mine using the conditional statement to accomplish essentially the same thing. The biggest difference is that he is building a string with a comma and space separating each value while I was building the querystring.

His code will work with a couple small fixes.

Code:
var s;
var o = document.forms['frmFilter'].elements['selCategory'].options;
for ( var i = 0; i < o.length; i++ ) {
    if ( o[i].selected ) s += (s.length > 0) ? (", " + [COLOR=red]o[i][/color].value) : [COLOR=red]o[i][/color].value;
}
alert (s);

Google, you're my hero!
 
Thanks guys. I'll poke around with these. cL: I see what you are getting at. Didn't work how I tried it, but I'll tinker. NightOwl, works like a charm. I'm going to try calling it as a function when parsing the entire form and run it for getElementByTag for selects and see if I can get that to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top