INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!

*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.

Jobs

Form Handling

Working with checkboxes on dynamic forms by DreXor
Posted: 25 Jun 04 (Edited 25 Jun 04)

part of the problem with checkboxes is, unless you make some wild adjustments, like javascript to update the value of the checkbox on submit, onvalue offvalue, or resort to radio buttons..

checkboxes not checked on submit have no name or value, they kind of dont exist.
so if you have a dataform ( populated from a DB ) and say you check a box, no problem , submit, update, it's in the db.

after it's been checked, however, and you remove the checkmark.

Unless you hardcode the form receipient to look for a blank value for the check box, you won't catch it as part of the form collection. So, no field value sent across to check against, to denote, this was "unchecked" update the db to reflect it.

the easiest workaround for this is to build a string array of the names of the checkbox fields when building the form :
** note : apply your form element naming conventions to the value stored, so you're only needing to request the value later.

CODE

'typically handled in some form of {for each field in rs.fields} loop
' this number will vary from datasource, number appropriately
If Field.DataType = ** Then
  StrCheckboxes = StrCheckBoxes & Field.Name & ","
End If

then placing this string into a hidden form element that you can reference:

CODE

<input type="hidden" name="checkboxvalidate" value="<%=strCheckBoxes%>">

after submit, split, and systematcally check for the checkboxes.

CODE

If Request("checkboxvalidate") <> "" then
  arrCheckBoxes = split(Request("checkboxvalidate"),",")
' on each checkbox de-apply naming convention in order to derivate the field name
  for each Check in arrCheckBoxes
    If Request(check) = "on" Then
      InsSqlCheckNames = strSqlChecks & check & ", "
      InsSqlCheckValues = strSqlChecks & "True, "
      UpdSqlChecks = strSqlChecks & check & "=True, "
    Else
      InsSqlCheckNames = strSqlChecks & check & ", "
      InsSqlCheckValues = strSqlChecks & "False, "
      UpdSqlChecks = strSqlChecks & check & "=False, "
    End If
  Next
End If
** in turn, when retrieving the other form elements, you can use this array as a filter for fields NOT to request, because they are handled seperately

when you're almost ready to send your constructed SQL statement to the DB insert the necessary variable values based on SQL action :
*note: the variables end with a trailing comma space, making them perfect for pre-insertion

CODE

If Action = "Update" then
  SQL = "Update <table> Set >" & UpdSqlChecks & "<othervalues Where <conditions>"
ElseIf Action = "Insert" then
  SQL = "Insert into <table>(>" & InsSqlCheckNames & "<fieldnames>) values(>" & InsSqlCheckValues & "<othervalues>)"
Else..~<crop>

Happy Coding!

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

My Archive

Resources

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