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

Request.Form(Item) and Checkboxes

Status
Not open for further replies.

SonJ

Programmer
Oct 24, 2002
166
GB
Hi there,

I have an ASP page which allows users to cancel registrations. At the moment, users are expected to search for a registration and for each result returned, a checkbox is returned with each of the results which has the value of "Yes" associated with it.

If the user checks the box and submits the form, it is assumed that they want to cancel the registration - which works fine.

If they want to reinstate the cancelled registration, then I want them to uncheck the box and submit the form so that the page can process the request.

The code snippet below shows the thought processes:

Code:
if Request.Form("txtUpdate")="XXX" then
  call OpenDatabase
  call CreateCommand
        
  dim Item, intIsCancelled
  
  for each item in Request.Form
    if left(item, 3)="chk" then
      dim strThisReferenceNumber, strSQL2
      strThisReferenceNumber=Mid(Item, 4, 7)
      if Request.Form(Item)="Yes" then
        intIsCancelled=1
      else
        intIsCancelled=0
      end if
      
      strSQL2="Update tblDelegates SET IsCancelled=" & intIsCancelled & " WHERE ReferenceNumber='" & strThisReferenceNumber & "'"
      objCmdTemp.CommandText=strSQL2
      objCmdTemp.Execute
    end if
  next
 call CloseDatabase
end if

I have put it in a loop as there is no way of knowing the number of records that would be returned by the search performed by the user.

Now, the problem that I am getting - and have just read about in the google groups, is that empty check boxes are not posted when the form is submitted.

I have been working on this for some time now, and really need some assistance!

Any help, pointers, references would be much appreciated!

SonD [hourglass]
 
Unchecked checkboxes ARE posted, they just don't have a value. You can specify what value a checkbox will return when checked in the html:

Code:
<input type=&quot;checkbox&quot; name=&quot;chk1&quot; value=&quot;test&quot;>

You will get
Code:
chk1=test
if it is checked and
Code:
chk1=
if it is not.

So really, your test for the checkbox in your code is correct. Something else must be causing the problem.
 
James,

Thanks for the feedback. Since posting the code here, I have managed to find a work around. Basically, I have added a hidden text field, which holds the original value of the IsCancelled field. When the user makes a request to update the the status of a registration then, I compare the current value to the original value, and only do the update if they are different to each other.

For anyone that is interested, the code is as follows:

Code:
if Request.Form(&quot;txtUpdate&quot;)=&quot;XXX&quot; then
  call OpenDatabase
  call CreateCommand
        
  dim Item
  
  for each item in Request.Form
    if left(Item, 3)=&quot;hdn&quot; then
      dim strThisReferenceNumber, strSQL2, strThisCheckbox, intPriorValue, intCurrentValue, intUpdatedValue
      strThisReferenceNumber=Mid(Item, 4, 7)
      
      strThisCheckBox=&quot;chk&quot; & strThisReferenceNumber
      
      intPriorValue=Request.Form(item)
      intCurrentValue=Request.Form(strThisCheckBox)
      if intCurrentValue=&quot;&quot; then intCurrentValue=&quot;0&quot;
      
      if intPriorValue<>intCurrentValue then
        'decide new value and do the update
        if intPriorValue=&quot;1&quot; then
          intUpdatedValue=&quot;0&quot;  
        else
          intUpdatedValue=&quot;1&quot;
        end if
        strSQL2=&quot;Update tblDelegates SET IsCancelled=&quot; & intUpdatedValue & &quot; WHERE ReferenceNumber='&quot; & strThisReferenceNumber & &quot;'&quot;
        objCmdTemp.CommandText=strSQL2
        objCmdTemp.Execute
      end if
      
    end if
  next
 call CloseDatabase
end if

It seems to work fine for now. I will take a look at it again to see if there is a better way of doing it.

Thanks for the input.

SonD [2thumbsup]

 
Unchecked checkboxes are NOT posted. You can check the Request collection for any item, and if that item does not exist than it returns an empty string as the value. Here is an example script, save it as sample.asp:
Code:
<%
Option Explicit

Dim item
For Each item in Request.Form
	Response.Write item & &quot; was in the form collection with a value of: [&quot; & Request.Form(item) & &quot;]<br>&quot;
Next

Response.Write &quot;<br>--------- Specifying the items by name -----------------<br>&quot;
Response.Write &quot;value of Request.Form(&quot;&quot;chkOne&quot;&quot;): [&quot; & Request.Form(&quot;chkOne&quot;) & &quot;]<br>&quot;
Response.Write &quot;value of Request.Form(&quot;&quot;chkTwo&quot;&quot;): [&quot; & Request.Form(&quot;chkTwo&quot;) & &quot;]<br>&quot;
Response.Write &quot;value of Request.Form(&quot;&quot;chkThree&quot;&quot;): [&quot; & Request.Form(&quot;chkThree&quot;) & &quot;]<br>&quot;
%>
<html>
<body>
<form action=&quot;sample.asp&quot; method=POST>
<input type=&quot;checkbox&quot; name=&quot;chkOne&quot; value=&quot;test&quot;>
<input type=&quot;checkbox&quot; name=&quot;chkTwo&quot; value=&quot;test&quot; checked>
<input type=&quot;submit&quot;>
</body>
</html>

The first output part of the ASP lists all the items in the Request.Form collection, the second part explicitly asks for the values of chkOne, chkTwo, and the non-existant chkThree.

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Thanks for the info Tarwn.

Sorry for the slight mis-information SonD. Your method of checking the value of the checkbox will still work though!
 
Actually, using the hidden field is usally the process I use as well. Now that I spent some more time looking at your code I can tell you what was happening.
I assume that basically anything you set to be cancelled was not getting updated correct?

What was happening is since you were looping through all the request.form collection and only doing your update when it found a form object starting with &quot;chk&quot;. Since your checkbox wouldn't pass a value if it was unchecked, it would never hit the
Code:
If Request.Form(Item)=&quot;Yes&quot; then
so it would never hit a case where it would go to the else because only if the checkbox existed would it get to the if statement, and the only way it would exist is if the value was yes.

A similar way to do this is to create a hidden field for the whole form. Then place onClick/onChange events on each of the inputs the user can edit. The onClick/onChange will simply add the reference number for the changed record to a hidden field (hdnChanges perhaps). When you get to your processing page all you have to do is split that string into an array, do a loop through the array updating for the reference numbers. In order to keep from duplicating updates (say if the person edited two fields) you can add each reference number to a temporary string with delimiters, say #'s. So when you update record with reference 1 your temp would be:
&quot;#1#&quot;
updating 2 would make it:
&quot;#1##2#&quot;

Then to double check a record before updating you could use an InStr to see if the array value is already in the temp string:
If Not InStr(tempString,&quot;#&quot; & myArray(i) & &quot;#&quot;) > 0 Then
'update this record, it's not in the temp string
End If

Not sure if this helps or not,
-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Hi Tarwn,

Thanks for the feedback. I was actually getting the error when I wanted to reinstate a registration - ie turn it from cancelled to 'not cancelled', the code failed to do the update. Now I know why!

When I realised what the problem was, I thought of using the JavaScript events you mentioned to try to solve it. I found using the onChange event somewhat temperamental when I was using it with a checkbox, or maybe it was just my dodgy code :). Anyhow, I am at a stage where I am happy with the way that it works.

Once again, thanks for the input. One day, hidden form fields will rule the world :)

SonD



 
Ahm, sorry about that, I was thinking in terms of 0 and 1, so since not cancelled is unchecked, than that was the ont that couldn't get to the update statement :p

OnChange uis definately a pain for checkboxes, use onClick for them because if you click a checkbox your changing it :)

Glad to have provided some food for thought and perhaps assistance with future projects,
-Tarwn
________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Hi, I have been having the same problem with a generic database editor I wrote. It was set to display a checkbox if it was a Boolean type field, but looping through the form collection, unchecked values weren't passed to the recordset update function. I was just about to try out your hidden field example, but solved the problem a lot simpler (for me anyway)...instead of using a checkbox, I now use a Radio button pair (both have the same name, but the values are Yes|No) - it works just fine and is less code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top