Web Analytics Made Easy -
StatCounter Checkbox Woes - ASP - CodingForum

Announcement

Collapse
No announcement yet.

Checkbox Woes - ASP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Checkbox Woes - ASP

    Looks good here!

    Hope someone can help me.

    I have 1 text field that i retrieve from a stored procedure. I loop through creating a new row for each result from the stored procedure. I now create a checkbox next to each result. For example, Text field is 'Description' and results are 'Fire Training' and 'Driver Training'. So i start to build up a list of descriptions with checkboxes next to them.

    When i submit the form i would like the checkbox to send a value of either 1 (checked) or 0 (unchecked) for each description.


    The problem that i am having it that when the checkbox is not checked it doesn't return a 0.

    Here is my code:

    The Description field and Checkbox
    <%
    'display all teams and allow the user to choose
    strSQLQuery = "spSelectBeforeArrival '" & strMNumber & "'"
    Set rsBeforeArrival = objADODBConnection.Execute(strSQLQuery)
    Do while not rsBeforeArrival.EOF
    %>

    <tr>
    <td width="85%" bgcolor="#F5F5F5"><INPUT type="hidden" name="txtDescription" value="<%=rsBeforeArrival("BeforeArrivalID")%>"><font size="1"><%=rsBeforeArrival("Description")%></font></td>
    <td width="15%" bgcolor="#F5F5F5"><input type="checkbox" name="chkNotify">
    </td>
    </tr>
    <%
    'move through recordset retrieving all rows
    rsBeforeArrival.MoveNext
    Loop
    rsBeforeArrival.Close
    %>

    When submitted:

    for i = 1 to Request.Form("txtDescription").count
    if Request.Form("chkNotify")(i) ="on" then
    blnNotify ="1"
    else
    blnNotify ="0"
    end if
    Next


    Any ideas what i am doing wrong?

  • #2
    when a checkbox is not checked, it is NOT submitted to the server.
    so you just check if a certain checkbox has value or not. If it has value, then the checkbox is checked, otherwise not.

    if request.form("chkNotify")<>"" then ...
    Glenn
    vBulletin Mods That Rock!

    Comment


    • #3
      Glenn,

      Cheers for your comment, i would like it to return a 0 if the checkbox isn't checked. So far i have:

      for i = 1 to Request.Form("txtDescription").count

      if request.form("chkNotify")(i)<> "" then
      blnNotify = "1"
      else
      blnNotify = "0"
      end if



      Response.Write blnNotify

      Next

      This returns a 1 if the checkbox is checked however it doesn't return a 0 if it isn't. Any ideas how to get it to return a 0 if the checkbox isn't checked?

      Ta

      Comment


      • #4
        Tried that earlier Dave, I had 5 check boxes to check and i checked 3. I got 3 1's and a error message:-

        111
        Request object error 'ASP 0105 : 80004005'

        Index out of range

        test.asp, line 52

        An array index is out of range.


        Is there any why to get the unchecked box to pass through a 0?

        Comment


        • #5
          When a checkbox is checked it will return the value of "on"

          If the checkbox is NOT checked, it will return no value.

          Check for that value with all 5 of your checkboxes...

          So, just name all of your checkboxes, do not give them a value, and in your ASP script, check for the "on" value.

          <%
          If LCase(Request.Form("Checkbox_Name")) = "on" Then
          Response.Write("Checkbox_Name was checked!")
          Else
          Response.Write("Checkbox_Name was NOT checked!")
          End If
          %>

          ~Quack
          Last edited by QuackHead; Jun 26, 2002, 10:08 AM.

          Comment


          • #6
            QuackHead,

            I have tried that but i just get the error stated about for when the checkbox isn't checked.

            Here is my code.

            <input type="checkbox" name="chkNotify">


            Next Page:

            for i = 1 to Request.Form("txtDescription").count

            If LCase(Request.Form("ChkNotify")(i)) = "on" Then
            Response.Write("Checkbox_Name was checked!")
            Else
            Response.Write("Checkbox_Name was NOT checked!")
            End If

            Next

            I get Checkbox_Name was checked! when it has been checked but get the error 'Index out of Range' when one isn't checked.

            Any more ideas?

            Comment


            • #7
              Just a slight clarification here . . .


              Request.Form returns a collection of name/value pairs representing the elements of a client form submitted via the POST method. BUT only controls with a valid name and value (a.k.a. successful controls) are submitted with the form data set. Unchecked checkboxes, even if they are named, are not sent with the form.

              This .aspx sample shows just that, but it's just the same for classic .asp.

              Enumerating the Request.Form Collection

              If you need some backgrounder on HTML forms, see:

              http://www.w3.org/TR/REC-html40/inte...html#h-17.13.2
              aspxtreme

              Comment


              • #8
                holty,

                can you please post the full error message (line number and everything)

                Also, where does txtDescription come from?

                ~Quack

                Comment


                • #9
                  You cannot use Request.Form to retrieve what is not in the collection in the first place.

                  holty, maybe if you could be a li'l clearer on what exactly you are trying to accomplish, someone out here may be able to help.
                  aspxtreme

                  Comment


                  • #10
                    I suspect that you have 5 checkboxes with the same name that's why you code it this way:

                    if Request.Form("chkNotify")(i) ="on" then ...

                    Checkboxes should not have the same name unlike radio buttons:

                    <input type="checkbox" name="chkNotify1">
                    <input type="checkbox" name="chkNotify2">
                    <input type="checkbox" name="chkNotify3">
                    <input type="checkbox" name="chkNotify4">
                    <input type="checkbox" name="chkNotify5">

                    for i = 1 to 5
                    if Request.Form("chkNotify" & i)<>"" then
                    response.write "Checkbox " & i & " is checked."
                    else
                    response.write "Checkbox " & i & " is not checked."
                    end if
                    next
                    Glenn
                    vBulletin Mods That Rock!

                    Comment


                    • #11
                      Guys, Thanks for all your help. As you may have guessed I find it hard to explain stuff!!.

                      Ok, basically i am working on a web app with a SQL background. Part of my web app is to record an employee's training. The training activity (e.g. fire training) is controlled by the end user and therefore can add as many training activities as they would like. The page that i am having problems with is 'Notify' page that the end user goes into and selects the employee and will then see a list of training activities (txtDescription) and a checkbox to notify (chkNotify). The number of training activities are controlled by the end user so thats why i loop through the recordset to return all the activities and draw a checkbox for each:-
                      <%
                      'display all teams and allow the user to choose
                      strSQLQuery = "spSelectBeforeArrival '" & strMNumber & "'"
                      Set rsBeforeArrival = objADODBConnection.Execute(strSQLQuery)
                      Do while not rsBeforeArrival.EOF
                      %>

                      <tr>
                      <td width="85%" bgcolor="#F5F5F5"><INPUT type="hidden" name="txtDescription" value="<%=rsBeforeArrival("BeforeArrivalID")%>"><font size="1"><%=rsBeforeArrival("Description")%></font></td>
                      <td width="15%" bgcolor="#F5F5F5"><input type="checkbox" name="chkNotify">
                      </td>
                      </tr>
                      <%
                      'move through recordset retrieving all rows
                      rsBeforeArrival.MoveNext
                      Loop
                      rsBeforeArrival.Close
                      %>

                      I would like to pass through 1's (for checked) and 0's for unchecked checkboxes so that i can send email (via CDONTS) when the checkboxes are checked.

                      It would also be ok if it just passed through the 1's for when it is checked and nothing when it isn't but i get the error:

                      "
                      111
                      Request object error 'ASP 0105 : 80004005'

                      Index out of range

                      test.asp, line 52

                      An array index is out of range.
                      "

                      This error occurs when i have 5 checkboxes and i check 3.

                      I am fair confused!!

                      Dave please can you explain with an example how to do:

                      "you can use hidden fields -- one for each checkbox and duplicately named (but not the same name as the checkboxes) -- and put your "1" and "0" values in them appropriately"

                      Thanks

                      Comment


                      • #12
                        Ok, I have just tried to pass through the value of the training activity. It works fine.

                        The problem that i was having is that i was doing

                        for i = 1 to Request.Form("txtDescription").count

                        Which would return 5 descriptions, but when a checkbox wasn't checked the index of the array was incorrect as i was doing

                        Response.write Request.Form("ChkNotify")(i)

                        So now i am doing

                        for i = 1 to Request.Form("txtDescription").count

                        Which will return the number of checked boxes.

                        It seems to be ok now. Thanks for all your help.

                        I will do some testing and if it fails i will tell you guys whats up!!

                        Comment


                        • #13
                          To know which checkbox is associated to a particular description, you must do this:

                          <%
                          'display all teams and allow the user to choose
                          ctr = 0
                          strSQLQuery = "spSelectBeforeArrival '" & strMNumber & "'"
                          Set rsBeforeArrival = objADODBConnection.Execute(strSQLQuery)
                          Do while not rsBeforeArrival.EOF
                          ctr = ctr + 1
                          %>

                          <tr>
                          <td width="85%" bgcolor="#F5F5F5"><INPUT type="hidden" name="txtDescription<%=ctr%>" value="<%=rsBeforeArrival("BeforeArrivalID")%>"><font size="1"><%=rsBeforeArrival("Description")%></font></td>
                          <td width="15%" bgcolor="#F5F5F5"><input type="checkbox" name="chkNotify<%=ctr%>">
                          </td>
                          </tr>
                          <%
                          'move through recordset retrieving all rows
                          rsBeforeArrival.MoveNext
                          Loop
                          rsBeforeArrival.Close
                          %>
                          <input type="hidden" name="ctr" value="<%=ctr%>">



                          in the submit page:

                          <%
                          intCtr = request.form("ctr")
                          if intCtr="" then
                          intCtr=0
                          else
                          intCtr = cint(intCtr)
                          end if

                          for i = 1 to intCtr
                          if request.form("chkNotify"&i)<>"" then
                          response.write "chkNotify" & i & " is checked.<br>"
                          'send mail, pass the corresponding description
                          sendMail(request.form("txtDescription" & i))
                          else
                          response.write "chkNotify" & i & " is not checked.<br>"
                          end if
                          next

                          'your send mail subroutine
                          sub sendMail(desc)

                          response.write desc & "<br>"

                          'your code here

                          end sub
                          %>


                          Originally posted by holty
                          Ok, I have just tried to pass through the value of the training activity. It works fine.

                          The problem that i was having is that i was doing

                          for i = 1 to Request.Form("txtDescription").count

                          Which would return 5 descriptions, but when a checkbox wasn't checked the index of the array was incorrect as i was doing

                          Response.write Request.Form("ChkNotify")(i)

                          So now i am doing

                          for i = 1 to Request.Form("txtDescription").count

                          Which will return the number of checked boxes.

                          It seems to be ok now. Thanks for all your help.

                          I will do some testing and if it fails i will tell you guys whats up!!
                          Glenn
                          vBulletin Mods That Rock!

                          Comment


                          • #14
                            I have another problem now! On the Update!

                            I have 5 checkboxes that have all been checked and exist in the database as 5 rows with a 1 in.

                            If i decide on the update to uncheck a box, how can i pass through the change so that the database can be updated (row deleted)

                            Comment


                            • #15
                              Think i have sorted it now, i will reset the bit field back to 0 and if they are checked it will update it back to 1. pheeeeeeeeew

                              Comment

                              Working...
                              X