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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the results from a group of checkboxes or a select multiple

Form Handling

How to get the results from a group of checkboxes or a select multiple

by  stakadush  Posted    (Edited  )
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
Code:
<%
If (isEmpty(Request.Form("books")) And isEmpty(Request.Form("music"))) Then
	Call PrintForm()
Else
	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>"
	Next

	Response.Write ("<br>")

	Response.Write ("<b>Music you like:</b><br>")
	For Each strMusic In arrMusic
		Response.Write strMusic & "<br>"
	Next
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
</select><br><br>

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>
</form>
<%End Sub%>
The first If..Else condition checks if the form was submitted. If the
Code:
Request.Form("values")
and
Code:
Request.Form("music")
variables are both empty, it means nothing was returned from the user so we print the form by calling the
Code:
PrintForm
subroutine.

Else, the form was submitted so we call the
Code:
PrintResults
subroutine. We use the
Code:
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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top