Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2003
    Posts
    20

    Exclamation Unanswered: problems updating checkboxes in form

    I'm having problems updating the checkboxes. Below is the code:

    If Request("chkCount") > 0 Then
    For i = 0 to Request("chkCount")
    chkID = Request("display" & i)

    If chkID <> "" Then
    objConn.Execute("UPDATE Downloads SET display = 1 WHERE id = " & chkID)
    Else
    objConn.Execute("UPDATE Downloads SET display = 0 WHERE id = " & i)
    End If
    Next

    sMsg = "Items have been successfully updated!"
    End If

    %>
    <form name="form1" action="downloads.asp" method="POST">
    <table border="0" cellpadding="0" cellspacing="0" width="414" >
    <tr>
    <td width="301">Select files: </td>
    </tr>
    <% rs.MoveFirst
    While Not rs.EOF
    iCount = iCount + 1
    %>
    <tr>
    <td width="541"><input type="checkbox" name="display<%=iCount%>" value = "<%=rs("id")%>" <% if rs("display") Then Response.Write "CHECKED=True" End If%> ><%=rs("filename")%> - <%=rs("title")%></td>
    </tr>
    <% rs.MoveNext
    Wend
    %>
    <input type="hidden" name="chkCount" value="<%=iCount%>">
    <tr height="43">
    <td height="43" colspan="3" width="474"><input type="submit" value="Select"><input type="reset"></td>
    </tr>
    </table>
    </form>


    When the a box is not check and I check it and click on the submit button, that record is updated. After it is checked, and when I try to uncheck it, the record is not updated. How can I update the record when the user unchecks the checkboxes?

    Thanks!

  2. #2
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    I'm not sure on this, it's been a while so forgive me if I'm wrong....

    If you request a unchecked tick box the value that is returned will be null. You have to specifically use the isNull() function to check if the value is null (checking by using <> "" won't work).

    Give that a try and see what happens.

  3. #3
    Join Date
    Feb 2002
    Posts
    2,232
    That should work - Try 2 things:

    1. Find out if it is going to the wrong condition - Add a response.write for each condition to see which one is getting it.
    2. If it is going to the wrong condition then response.write out the value of the checkbox with leading and trailing characters to see if there is a value given.

  4. #4
    Join Date
    Dec 2003
    Posts
    454

    the problem may be the counters

    The two counters do not match. In the For ... Next loop, you used i starting with 0 to Request("chkCount"), but in the form, you placed iCount = iCount + 1 at the beginning of the While ... Wend loop. That means the starting number is 1 if iCount was initialized to 0. You may try to move iCount = iCount + 1 to the end of loop, which is before rs.MoveNext.

  5. #5
    Join Date
    Feb 2002
    Posts
    2,232
    As gyuan already mentioned, your counters are out of sink - but I think the problem is that your update statement uses 2 different variables to base the update:

    objConn.Execute("UPDATE Downloads SET display = 1 WHERE id = " & chkID)
    objConn.Execute("UPDATE Downloads SET display = 0 WHERE id = " & i)

    When executing updates based on the same column - you need to keep the same variable. Your rs("id") holds the key to this - what are the values for rs("id") ? Your id value may be different from your counter value which could be causing the problem.

    So zero base your iCount while loop or 1-base your for-next loop and change chkID to i for both update statements in the where clause.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •