How do I loop through a comma-separated list?

C

Christina

I have a post-form that holds a listbox with mulitple selected items. When
the form is posted to the server ASP file, I want to loop through the
selected items, to insert each of them into a table. How do I do that?

If I execute the line:
Response.Write(Request.Form("ListBox")),
I get the list of selected values, separated by commas.


Christina
 
R

Ray at

I believe that the items are probably separated by a comma and a space
actually, yes? What you'd want to do is create an array.

<%
sVal = Request.Form("Listbox")
''sVal = "Joe, Bob, Tom, Phil, Toby" for argument's sake

ArrayOfValues = Split(sVal, ", ")

For i = 0 To UBound(ArrayOfValues)
Response.Write "Value " & i & " is " & ArrayOfValues(i) & "<br>"
Next
%>

Ray at work
 
U

UncleWobbly

if you then split that list into an array yo then only need a simple loop to
grab 'em all:

myarray=split(Request.Form("ListBox"))

for n=0 to ubound(myarray)
' ... now each of the items is accessed as ;
myarray(n)
next
 
T

TomB

While admittedly Ray's solution is the correct one. If you are just putting
them in a table, you could do something like....

<table>
<tr>
<%
sVal=Request.Form("Listbox")
Response.Write "<td>" & Replace(sVal,", ","</td><td>") & "</td>"
%>
</tr>
</table>
 
R

Ray at

What I'm wondering is if the separator is done by the server or if the
browser chooses the delimiter and posts the data with it. I ~believe~ that
the browser submits data like:

select=a&select=b&select=c, etc. and it's the ASP process that returns that
as comma+space delimited values.

Ray at work
 
C

Chris Hohmann

Ray at said:
I believe that the items are probably separated by a comma and a space
actually, yes? What you'd want to do is create an array.

<%
sVal = Request.Form("Listbox")
''sVal = "Joe, Bob, Tom, Phil, Toby" for argument's sake

ArrayOfValues = Split(sVal, ", ")

For i = 0 To UBound(ArrayOfValues)
Response.Write "Value " & i & " is " & ArrayOfValues(i) & "<br>"
Next
%>

Ray at work

I believe Request.Form(Key) is already an array (sort of):

<%
Dim i,iMax
iMax = Request.Form("test").Count
For i = 1 To iMax
Response.Write Request.Form("test")(i) & "<br>"
Next
%>
<form method="post">
<select name="test" multiple="multiple">
<option value="apple">apple</option>
<option value="banana">banana</option>
<option value="cranberry">cranberry</option>
<option value="danish">danish</option>
<option value="egg">egg</option>
</select>
<input type="Submit">
</form>

The documentation states as much. However, what is not made clear is
that there is an implicit translation to a string when a form variable
is referenced. As such the following fails with a type mismatch:

UBound(Request.Form("test"))

Here's a link to the documentation:
http://msdn.microsoft.com/library/en-us/iissdk/iis/ref_vbom_reqocf.asp


HTH
-Chris
 
R

Roland Hall

:
: of course that should read
:
: myarray=split(Request.Form("ListBox"),",")

or:
myarray=split(Request.Form("ListBox"),", ")
....if there is actually a space in it as Ray mentioned.

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
 
R

Ray at

Actually, I think I now prefer the method Chris posted, because if one of
your option values is "B, C" that'll be split. I am so finished with
splitting multi-element form controls! :]

Ray at work
 
C

Christina

Thank you all for good help!! :)
This worked:

myArray = Request.Form("ListBox").Split(",")
For i = 0 To UBound(myArray)
Response.Write(myArray.GetValue(i))
Next


Hugs,
Christina
 
A

anherman

Hello Christina,

I don't know in what environment you are programming but you might be able to use this HTML and JavaScript code to show how to separate the comma separated list into an array. The split() method creates an array of elements from a String object by using the delimiter specified in its parameter. The code may not be perfect because I wrote it during my computer lab class in college so maybe there can be some improvements. I am just showing you an idea. Use the array then to fill up a table.

Signed,
BusyMan

CODE******************************************
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
var sampleText="red,green,blue,violet";

function getWords() {
var input = sampleText.split(",");
var input2 = new String();
for (var i = 0; i<input.length; i++) {
input2 += input + "<BR>";
}
document.all("HERE").innerHTML = input2;
}

</SCRIPT>
</HEAD>
<BODY onLoad="getWords()">
<SPAN ID="HERE"></SPAN>
</BODY>
</HTML>

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
U

UncleWobbly

:eek:)

not yet you aren't... see Christina's final solution :eek:)

Ray at said:
Actually, I think I now prefer the method Chris posted, because if one of
your option values is "B, C" that'll be split. I am so finished with
splitting multi-element form controls! :]

Ray at work

UncleWobbly said:
play safe and trim the items in the array as they are used :eek:)


useful,
but
 
R

Ray at

This worked:

myArray = Request.Form("ListBox").Split(",")
For i = 0 To UBound(myArray)
Response.Write(myArray.GetValue(i))
Next

That worked? What language is this? VBJscript? ? ? ?

Ray at work
 
B

Bob Barrows

Christina said:
Thank you all for good help!! :)
This worked:

????
How could this possibly work?
myArray = Request.Form("ListBox").Split(",")

A JScript function, Split(), followed by
For i = 0 To UBound(myArray)

A vbcript-style loop through an array using a vbscript function, ubound()
followed by
Response.Write(myArray.GetValue(i))

another jscript function, GetValue(), which is syntactically incorrect
(getValue() is correct) to get the value of the array element!

Bob Barrows
 
J

Jeff Cochran

That worked? What language is this? VBJscript? ? ? ?

Now if there were a VBPB&Jscript that would also trim the cursts I'd
be impressed... :)

Actually, I am impressed with Christina's solution. I don't know
enough to say why it works, but it opens possibilities I hadn't
considered before. Good work.

Jeff
 
R

Ray at

Actually, I am impressed with Christina's solution. I don't know
enough to say why it works, but it opens possibilities I hadn't
considered before.

What is going on here? Since when does request.form have a split method?
Is today April 1st? Did I miss a post somewhere or something?

Ray at work
 
J

Jeff Cochran

What is going on here? Since when does request.form have a split method?
Is today April 1st? Did I miss a post somewhere or something?

It doesn't. Which is kinda the beauty of it. It uses a method that
isn't there. Very Zen. Hmmm... Don't code the method, *Be* the
method... :)

Nuts, you're right. It isn't April 1...

Jeff
 
R

Roland Hall

: Thank you all for good help!! :)
: This worked:
:
: myArray = Request.Form("ListBox").Split(",")
: For i = 0 To UBound(myArray)
: Response.Write(myArray.GetValue(i))
: Next

It's a new language, all it's own. (see undocumented feature set)
What will Christina come up with next? (O:=

Roland (confused - as I should be)
 

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

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top