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

checked all dynamic generated checkboxes at once 2

Status
Not open for further replies.

necta

Technical User
Mar 8, 2004
35
MY
Hi,
I have a list of checkboxes. When the top most checkbox is checked, all the checkboxes below it will be checked as well, just like what has in mail in yahoo. The checkboxes below the topmost is dynamically generated from record. I need a few pointers from the group members on how to get the dynamic generated checkboxes name. Thank you.

code snippets for checkbox:
<input type="checkbox" name="stud<%=studNo%>" value="<%=RS("ID")%>

rgrds,
Necta
 
I can think of two methods off the top of my head.
The fist would e to loop through all of the items in the Request.Form collection to see if they have the checkbox prefix your using in their name:
Code:
Dim rItem
For Each rItem in Request.Form
   'if the name starts with "stud"
   If left(rItem,4) = "stud" Then
      'do something with this
      Response.Write "Checkbox " & rItem& " was checked, value: " & Request.Form(rItem) & "<br>"
   End If
Next

The other method depends on where that StudNo came from. If you used ome sort of for loop or counter to create the checkboxes you could add an additional hidden field to the end of the form that passes the last value of the counter. Then you could simply loop from te starting number to that last number to see if the checkbox passed a value:
Code:
For i = 0 To Request.Form("MaxStudNo")
   If Request.Form("stud" & i) <> "" Then
      'this checkbox was checked, do something with it
      Response.Write "Checkbox stud" & i & " was checked, value: " & Request.Form("stud" & i) & "<br>"
   End If
Next

I put together a quick example that uses both of these mthods so you could see them in action (and I could test my on the fly code writing skills :) ):
Code:
<%
Option Explicit
'Sample Script to show finding checked checkboxes

'on first run build random number of checkboxes to use in examples
If Request.Form("flag") = "" Then
	'Build Some checboxes
	Dim maxNum, chkCounter
	Randomize
	maxNum = Int(Rnd() * 20)

	%>
	<html><body>
	This is the form, check some checkboxes:<br>
	<form method="POST" action="<%=Request.ServerVariables("SCRIPT_NAME")%>">
	<input type="hidden" name="flag" value="whatever">
	<%
	For chkCounter = 0 to MaxNum
		Response.Write "Checkbox #" & chkCounter & "<input type=""checkbox"" name=""stud" & chkCounter & """ value=""whatever""><br>"
	Next

	Response.Write "<input type=""hidden"" name=""MaxStudNo"" value=""" & MaxNum & """>"
	Response.Write "<input type=""submit"" value=""Submit""></form></body></html>"
Else
	Response.Write "<html><body>"

	'Process the form, find the checkboxes that were checked

	'METHOD #1: loop through entire request collection
	Dim rItem
	Response.Write "<b>Results of Method #1</b><br>"
	For Each rItem in Request.Form
		'if the name starts with "stud"
		If len(rItem) > 4 Then
			If left(rItem,4) = "stud" Then
				'do something with this
				Response.Write "Checkbox " & rItem & " was checked, value: " & Request.Form(rItem) & "<br>"
			End If
		End If
	Next

	'METHOD #2: loop based on counter
	Dim i
	Response.Write "<b>Results of Method #2</b><br>"
	For i = 0 To Request.Form("MaxStudNo")
		If Request.Form("stud" & i) <> "" Then
			'this checkbox was checked, do something with it
			Response.Write "Checkbox stud" & i & " was checked, value: " & Request.Form("stud" & i) & "<br>"
		End If
	Next

	Response.Write "</body></html>"
End If
%>

Copy it, save it, run it, name shouldn't matter. Hope this helps,

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Thanks a lot, Tarwn. I will on it..

Necta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top