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!

Working with checkboxes on dynamic forms

Form Handling

Working with checkboxes on dynamic forms

by  DreXor  Posted    (Edited  )
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!
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