Wrox Programmer Forums
|
ASP Forms As of Oct 5, 2005, this forum is now locked. Please use "Classic ASP beginner" at http://p2p.wrox.com/forum.asp?FORUM_ID=54 or "Classic ASP Professional" http://p2p.wrox.com/forum.asp?FORUM_ID=56 instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Forms section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 13th, 2003, 10:27 PM
Registered User
 
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Looping through an HTML select box

I'm trying to loop through an HTML Select box, and iterate through each selection chosen.

Each selection is separate by a comma, and I'm trying to break each into it's own line...the code below works if I don't use the Do While.

Each selection is 4 characters long, plus a comma. So for example, this is what it would look like: 1234, 2345, 6789. But I'm trying to get it to look like this:
1234
5678
6789

Thanks for any help!

The Code:

    Do While len(strProducts) >= 4

    commaPos = InStr(strProducts, ",")

    If commaPos = 0 Then
        value = strProducts
        strProducts = " "
    Else
        value = Mid(strProducts, commaPos-4, 4)

        strProducts = Mid(strProducts, commaPos+2, Len(strProducts)-6)

        Response.Write value & "<br>"
        Response.Write strProducts & "<br>"

    End If
    Loop

 
Old October 14th, 2003, 12:03 AM
Authorized User
 
Join Date: Sep 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to balakumar1000
Default

Why don't u use the methods of Select Element to get the selected values.

Balakumar V.

 
Old October 14th, 2003, 02:00 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

It may be a lot easier to use the Split method. This method splits the Request.Form value (or any other string) based on a comma (or any other separator) and returns an array of the items in the string

You can then simply loop through the array, displaying each selected item. The following code shows you how this works:
Code:
<body>
<%
    If Request.Form("btnSubmit") <> "" Then
        Dim arrSelection
        Dim iLoop
        Dim iUBound
        arrSelection = Split(Request.Form("lstTest"), ",")
        iUBound = UBound(arrSelection) ' The last item in the array
            ' with a zero based index, so an UBound of 2 means 3 items
        For iLoop = 0 to iUBound
            Response.Write("Selected Item " & arrSelection(iLoop) & "<br />")
        Next
    End If
%>
<form id="frmTest" name="frmtest" method="post" action="test.asp">
    <select name="lstTest" multiple="multiple" size="5">
        <option value="1234">1</option>
        <option value="2345">2</option>
        <option value="3456">3</option>
        <option value="4567">4</option>
        <option value="5678">5</option>
    </select>
    <input type="submit" name="btnSubmit" value="submit">
</form>
</body>
Hope this helps,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 15th, 2003, 01:18 AM
Registered User
 
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar...works, and it is a lot easier to do...

t~






Similar Threads
Thread Thread Starter Forum Replies Last Post
Fill select box and select recordset value markd Classic ASP Databases 1 February 20th, 2006 06:41 PM
html:select aytacakin Pro JSP 0 November 15th, 2005 04:58 AM
html:select aytacakin J2EE 0 November 14th, 2005 09:46 AM
multicolored List Box or Select Box sasidhar79 Javascript 1 February 15th, 2005 01:47 AM
select box/List box alphabetic sort sasidhar79 Javascript How-To 3 November 10th, 2004 03:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.