Desperately: Need help selecting a checkbox

Discussion in 'ASP General' started by Rodney King, Oct 12, 2004.

  1. Rodney King

    Rodney King Guest

    Hi,

    I have developed an ASP page which dynamically displays a list of
    checkbox options based on a SQL statement. Here is my code:

    <div style="OVERFLOW:auto; Height: 150px">
    <table>
    <%
    dim adOpenForwardOnly, adLockReadOnly
    dim adCmdTable, ctr, checkboxID
    adOpenForwardOnly = 0
    adLockReadOnly = 1
    adCmdTable = 2

    dim objConn, objRS, cmdType

    set objConn = Server.CreateObject("ADODB.Connection")
    objConn.Open("tanklink")
    set objCommand = Server.CreateObject("ADODB.Command")
    objCommand.ActiveConnection = objConn
    objCommand.CommandType = 1
    objCommand.CommandTimeout = 10
    objCommand.CommandText = Session("newSQL")

    objCommandType = adCmdText
    set objRS = objCommand.Execute

    while not objRS.EOF
    ctr = ctr + 1
    checkboxID = "tank_chkbox"
    Response.Write "<tr>"
    Response.Write "<td width='20px'>"
    Response.Write "<input type='checkbox' name='"
    & checkboxID & "' id='" & checkboxID & "' value='" & objRS("tankid")
    &"' />"
    Response.Write "</td>"
    Response.Write "<td width='200px'>"
    Response.Write objRS("Name")
    Response.Write "</td>"
    Response.Write "</tr>"
    objRS.MoveNext
    wend
    objRS.Close
    objConn.Close
    set objRS = Nothing
    set objConn = Nothing
    %>
    </table>
    </div>

    My code displays properly, but I am having trouble writing a function
    or a procedure that will parse through the list of checkboxes to see
    which checkbox is checked when a submit button is pressed. Can
    someone please provide some sample code to get me started?

    Response.Form("tank_chkbox") does not seem to work and gives me an
    error when I try and use it.

    Thanks
    Rodney King, Oct 12, 2004
    #1
    1. Advertising

  2. > Response.Form("tank_chkbox") does not seem to work and gives me an
    > error when I try and use it.


    What does "does not seem to work" mean? What is "an error", could you be
    more specific? What does "try and use it" mean, can you show what code
    you've tried?

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)
    Aaron [SQL Server MVP], Oct 12, 2004
    #2
    1. Advertising

  3. Rodney King

    Mark Schupp Guest

    It appears from your code that the checkbox name is the same in all cases.
    Is that your intent or were you planning to append the count in ctr to each
    name.

    If the checkboxes are all the same name then you will receive a
    comma-separated list of the values for all the "checked" boxes. You can
    access the individual values by splitting the list on comma or by iterating
    through the form collection as in:

    'split into array
    avalues = split(request.form("tank_chkbox"), ",")

    'iterate through form field
    For i = 0 to request.form("tank_chkbox").count 'may need to start with 1
    instead of 0
    value = request.form("tank_chkbox")(i)

    'do something with value
    Next

    Note: if your values may contain commas then you will need to use the
    request.form method instead of split

    If you are going to append the counter then you will need to loop through
    the possible counter values and check for form fields.

    --
    Mark Schupp
    Head of Development
    Integrity eLearning
    www.ielearning.com


    <Rodney King> wrote in message
    news:...
    > Hi,
    >
    > I have developed an ASP page which dynamically displays a list of
    > checkbox options based on a SQL statement. Here is my code:
    >
    > <div style="OVERFLOW:auto; Height: 150px">
    > <table>
    > <%
    > dim adOpenForwardOnly, adLockReadOnly
    > dim adCmdTable, ctr, checkboxID
    > adOpenForwardOnly = 0
    > adLockReadOnly = 1
    > adCmdTable = 2
    >
    > dim objConn, objRS, cmdType
    >
    > set objConn = Server.CreateObject("ADODB.Connection")
    > objConn.Open("tanklink")
    > set objCommand = Server.CreateObject("ADODB.Command")
    > objCommand.ActiveConnection = objConn
    > objCommand.CommandType = 1
    > objCommand.CommandTimeout = 10
    > objCommand.CommandText = Session("newSQL")
    >
    > objCommandType = adCmdText
    > set objRS = objCommand.Execute
    >
    > while not objRS.EOF
    > ctr = ctr + 1
    > checkboxID = "tank_chkbox"
    > Response.Write "<tr>"
    > Response.Write "<td width='20px'>"
    > Response.Write "<input type='checkbox' name='"
    > & checkboxID & "' id='" & checkboxID & "' value='" & objRS("tankid")
    > &"' />"
    > Response.Write "</td>"
    > Response.Write "<td width='200px'>"
    > Response.Write objRS("Name")
    > Response.Write "</td>"
    > Response.Write "</tr>"
    > objRS.MoveNext
    > wend
    > objRS.Close
    > objConn.Close
    > set objRS = Nothing
    > set objConn = Nothing
    > %>
    > </table>
    > </div>
    >
    > My code displays properly, but I am having trouble writing a function
    > or a procedure that will parse through the list of checkboxes to see
    > which checkbox is checked when a submit button is pressed. Can
    > someone please provide some sample code to get me started?
    >
    > Response.Form("tank_chkbox") does not seem to work and gives me an
    > error when I try and use it.
    >
    > Thanks
    >
    Mark Schupp, Oct 12, 2004
    #3
  4. Rodney King

    Rodney King Guest

    Mark,

    Thanks for responding. My problem is I can't even get the comma
    seperated list. I'm very familiar with ASP.NET but am new to ASP and
    JavaScript. What I essentially want to do is to have a submit button
    onclick event iterate through the table collection that I created and
    select the values of my checked checkboxes.

    Here is the code I have for my submit button and function:
    <script language="JavaScript">
    function display_checkbox_values()
    {
    var selected_list;
    selected_list = Response.Form("tank_chkbox");
    alert(selected_list);
    }
    </script>
    <td><INPUT type="submit" onclick="display_checkbox_values()"
    id=submit1 name=submit1>
    </td>

    When this code is run, it gives me an error.

    Aaron,

    Thanks for responding. The error message that I am getting is
    Response is not defined.


    Thanks for all your help.

    On Tue, 12 Oct 2004 13:46:37 -0700, "Mark Schupp"
    <> wrote:

    >It appears from your code that the checkbox name is the same in all cases.
    >Is that your intent or were you planning to append the count in ctr to each
    >name.
    >
    >If the checkboxes are all the same name then you will receive a
    >comma-separated list of the values for all the "checked" boxes. You can
    >access the individual values by splitting the list on comma or by iterating
    >through the form collection as in:
    >
    >'split into array
    >avalues = split(request.form("tank_chkbox"), ",")
    >
    >'iterate through form field
    >For i = 0 to request.form("tank_chkbox").count 'may need to start with 1
    >instead of 0
    > value = request.form("tank_chkbox")(i)
    >
    > 'do something with value
    >Next
    >
    >Note: if your values may contain commas then you will need to use the
    >request.form method instead of split
    >
    >If you are going to append the counter then you will need to loop through
    >the possible counter values and check for form fields.
    Rodney King, Oct 13, 2004
    #4
  5. (a) there is no such thing as response.form
    (b) you are mixing client-side and server-side script. Maybe you meant:

    var selected_list = "<%=Request.Form("tank_chkbox")%>";

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)




    <Rodney King> wrote in message
    news:...
    > Mark,
    >
    > Thanks for responding. My problem is I can't even get the comma
    > seperated list. I'm very familiar with ASP.NET but am new to ASP and
    > JavaScript. What I essentially want to do is to have a submit button
    > onclick event iterate through the table collection that I created and
    > select the values of my checked checkboxes.
    >
    > Here is the code I have for my submit button and function:
    > <script language="JavaScript">
    > function display_checkbox_values()
    > {
    > var selected_list;
    > selected_list = Response.Form("tank_chkbox");
    > alert(selected_list);
    > }
    > </script>
    > <td><INPUT type="submit" onclick="display_checkbox_values()"
    > id=submit1 name=submit1>
    > </td>
    >
    > When this code is run, it gives me an error.
    >
    > Aaron,
    >
    > Thanks for responding. The error message that I am getting is
    > Response is not defined.
    >
    >
    > Thanks for all your help.
    >
    > On Tue, 12 Oct 2004 13:46:37 -0700, "Mark Schupp"
    > <> wrote:
    >
    > >It appears from your code that the checkbox name is the same in all

    cases.
    > >Is that your intent or were you planning to append the count in ctr to

    each
    > >name.
    > >
    > >If the checkboxes are all the same name then you will receive a
    > >comma-separated list of the values for all the "checked" boxes. You can
    > >access the individual values by splitting the list on comma or by

    iterating
    > >through the form collection as in:
    > >
    > >'split into array
    > >avalues = split(request.form("tank_chkbox"), ",")
    > >
    > >'iterate through form field
    > >For i = 0 to request.form("tank_chkbox").count 'may need to start with 1
    > >instead of 0
    > > value = request.form("tank_chkbox")(i)
    > >
    > > 'do something with value
    > >Next
    > >
    > >Note: if your values may contain commas then you will need to use the
    > >request.form method instead of split
    > >
    > >If you are going to append the counter then you will need to loop through
    > >the possible counter values and check for form fields.

    >
    Aaron [SQL Server MVP], Oct 13, 2004
    #5
  6. Rodney King

    Rodney King Guest

    Thanks Aaron,

    I modified my JavaScript so that it looks like this:

    <script language="JavaScript">
    function display_checkbox_values()
    {
    var selected_list = "<%=Request.Form("tank_chkbox")%>";
    alert(selected_list);
    }
    </script>

    When I click on my submit button I no longer get an error message but
    my alert window never fires. For now I just want to see if I'm
    capturing anything.

    Thanks
    On Wed, 13 Oct 2004 09:53:31 -0400, "Aaron [SQL Server MVP]"
    <> wrote:

    >(a) there is no such thing as response.form
    >(b) you are mixing client-side and server-side script. Maybe you meant:
    >
    >var selected_list = "<%=Request.Form("tank_chkbox")%>";
    Rodney King, Oct 13, 2004
    #6
  7. Ah, I see. Once again, you are confusing server-side and client-side
    script. Request.Form("anything") will not be available until you actually
    submit the form, so you can only do your alert on the receiving page (not on
    the same page that holds the checkboxes). You will need to use client-side
    script if you want to alert which checkboxes are checked BEFORE submitting
    the form.

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)




    <Rodney King> wrote in message
    news:...
    > Thanks Aaron,
    >
    > I modified my JavaScript so that it looks like this:
    >
    > <script language="JavaScript">
    > function display_checkbox_values()
    > {
    > var selected_list = "<%=Request.Form("tank_chkbox")%>";
    > alert(selected_list);
    > }
    > </script>
    >
    > When I click on my submit button I no longer get an error message but
    > my alert window never fires. For now I just want to see if I'm
    > capturing anything.
    >
    > Thanks
    > On Wed, 13 Oct 2004 09:53:31 -0400, "Aaron [SQL Server MVP]"
    > <> wrote:
    >
    > >(a) there is no such thing as response.form
    > >(b) you are mixing client-side and server-side script. Maybe you meant:
    > >
    > >var selected_list = "<%=Request.Form("tank_chkbox")%>";

    >
    Aaron [SQL Server MVP], Oct 13, 2004
    #7
  8. Rodney King

    Rodney King Guest

    Thanks alot Aaron. That really helped. I'm finally beginning to get
    it. :)
    On Wed, 13 Oct 2004 11:20:16 -0400, "Aaron [SQL Server MVP]"
    <> wrote:

    >Ah, I see. Once again, you are confusing server-side and client-side
    >script. Request.Form("anything") will not be available until you actually
    >submit the form, so you can only do your alert on the receiving page (not on
    >the same page that holds the checkboxes). You will need to use client-side
    >script if you want to alert which checkboxes are checked BEFORE submitting
    >the form.
    Rodney King, Oct 13, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TGlvcg==?=

    Desperately need help finding connection leak

    =?Utf-8?B?TGlvcg==?=, Oct 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,671
    David Browne
    Oct 30, 2004
  2. Joey Liang via DotNetMonster.com

    Desperately need help in drop down list

    Joey Liang via DotNetMonster.com, May 25, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    757
    =?Utf-8?B?RXRoZW0gQXp1bg==?=
    May 26, 2005
  3. peshekeedweller

    sqlserver connection - need help desperately!

    peshekeedweller, Sep 15, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    631
    Juan T. Llibre
    Sep 18, 2005
  4. Replies:
    8
    Views:
    373
  5. Ben Fidge
    Replies:
    0
    Views:
    341
    Ben Fidge
    Aug 10, 2006
Loading...

Share This Page