Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 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
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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 ...

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    344
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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; 06-26-2002 at 03:08 PM.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #7
    New Coder
    Join Date
    Jun 2002
    Location
    Pilipinas
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Posts
    344
    Thanks
    0
    Thanked 0 Times in 0 Posts
    holty,

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

    Also, where does txtDescription come from?

    ~Quack

  • #9
    New Coder
    Join Date
    Jun 2002
    Location
    Pilipinas
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!!

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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!!

  • #14
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

  • #15
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

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