Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Microsoft: ASP (Active Server Pages) FAQ

Form Handling

How to get the results from a group of checkboxes or a select multiple by stakadush
Posted: 13 Oct 01

A common problem is when you want to get the results from a select multiple or a group of checkboxes with the same name.
You'd be happy to know it's quite simple. :)

Basically what happens is that the values are sent from the form seperated by a comma followed by a space (ie. ", ").You can use the Split function to place the values inside an array and then loop through the array and process each value.

Example Code

If (isEmpty(Request.Form("books")) And isEmpty(Request.Form("music"))) Then
    Call PrintForm()
    Call PrintResults()
End If

Sub PrintResults()
    Dim arrBooks,arrMusic
    arrBooks = Split(Request.Form("books"), ", ")
    arrMusic = Split(Request.Form("music"), ", ")

    Response.Write ("<b>Books you like:</b><br>")
    For Each strBook In arrBooks
        Response.Write strBook & "<br>"

    Response.Write ("<br>")

    Response.Write ("<b>Music you like:</b><br>")
    For Each strMusic In arrMusic
        Response.Write strMusic & "<br>"
End Sub

Sub PrintForm()%>
<form method=post>
What books do you like?<br>
<small>Please select all that apply</small><br>
<select name=books multiple>
<option value="Novel"> Novel
<option value="Fiction"> Fiction
<option value="Science Fiction"> Science Fiction
<option value="True_Stories"> True Stories
<option value="Fairy_Tales"> Fairy Tales

What music do you like?<br>
<small>Please select all that apply</small><br>
<input type=checkbox name=music value="Rock"> Rock<br>
<input type=checkbox name=music value="Country"> Country<br>
<input type=checkbox name=music value="Jazz"> Jazz<br>
<input type=checkbox name=music value="Techno"> Techno<br>
<input type=checkbox name=music value="Hard_Rock"> Hard Rock<br>
<input type=checkbox name=music value="World"> World<br><br>

<input type=submit>
<%End Sub%>

The first If..Else condition checks if the form was submitted. If the Request.Form("values") and Request.Form("music") variables are both empty, it means nothing was returned from the user so we print the form by calling the PrintForm subroutine.

Else, the form was submitted so we call the PrintResults subroutine. We use the Split function to create an array with all the values the user checked. We then loop through the array and print the values, one per line.

For further information be sure to read the article "How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query?" (http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=153) on 4GuysFromRolla.com

Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close