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

Dynamic Checkboxes

Status
Not open for further replies.

mrDrive

MIS
Aug 29, 2002
94
US
Hi,

I've got a page with checkboxes that are created dynamically from a recordset. Each is given a unique name as they are created. I'm scratching my head as to how to request values from these checkboxes on my "action" page, when the number of checkboxes on the page will vary from 1-8.

Can anyone direct me to an example that could help me with this?

Any help would be great!

Thanks,

mD
 
The great thing with checkboxes... if you place a check in one, then submit the form, you will find that you can test for the checkbox presence server-side.

If you don't place a check in a checkbox, then submit... you will not be see any mention of it server-side.

I'm assuming you give them predicable names (myChk1, myChk2, .. myChk8). Then with the knowledge above, you could just loop through all 8 possible checkbox names. If the names exist, then they were checked, if not... they weren't.

Does this make sense?

Jeff
 
Cool.

I guess I could just loop through all the possible chk names server-side, getting any checked values.

Thanks for the suggestion!
 
Also, if you don't know whether you will have 1 or 8 checkboxes (or anything in between): as long as you're creating the checkboxes dynamically, you can also dynamically create a hidden input field called 'numberOfCheckboxes' and set the value equal to the number of checkboxes created. This value will tell you how many you should check for on the other end.

Good luck!

--Dave
 
Nice suggestion Dave... sometimes the most obvious solutions get lost in the detail :)

Jeff
 
If you wanted to name them with the same name, you could use a For..Each loop in ASP:
Code:
[COLOR=black yellow]<%[/color]
For Each checkbox In Request.Form("myCheckBoxGroup")
  [green]'Do something[/green]
Next
[COLOR=black yellow]%>[/color]

Or if you named them myChk1,myChk2,etc..you could loop through all the fields:
Code:
[COLOR=black yellow]<%[/color]
For Each Field In Request.Form
  If InStr(Field,"myChk")>0 Then
    chkID=Mid(Field,6)
    Response.Write "Checkbox " & chkID & " = " & Request.Form(Field)
  End If
Next
[COLOR=black yellow]%>[/color]
This way it'll work even if they disable JavaScript.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top