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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts

    how to store 2 checkbox values in separate lines i

    Hello,

    I created a checkbox that allows users to select more than one boxes and use Access db to store values. How do I make 2 lines in Access table field if a user select 2 boxes.

    Here is my code:
    <input type="checkbox" name="a" id="a" value="Asian" />Asian<BR />
    <input type="checkbox" name="a" id="b" value="Latino" />Latino<BR />
    <input type="checkbox" name="a" id="c" value="White" />White<BR />

    If a user check 2 first boxes, I like to have a value in the field to be:
    Asian
    Latino

    Instead of Asian, Latino

    How do we do that?

    thanks.

  • #2
    New Coder
    Join Date
    Jun 2007
    Location
    VA
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure about parsing the values out in asp (I'm a PHP guy), but here's how to create an array for checkbox values:

    Code:
    <input type="checkbox" name="a[]" id="a" value="Asian" />Asian<BR />
    <input type="checkbox" name="a[]" id="b" value="Latino" />Latino<BR />
    <input type="checkbox" name="a[]" id="c" value="White" />White<BR />
    then you will have:

    array a(
    [0] => Asian
    [1] => Latino
    [2] => White
    )
    Last edited by ddanatzko; 06-01-2007 at 07:33 PM. Reason: typo

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You can replace the comma with newline.
    Code:
    dim races
    races = Request.Form("a")
    races = Replace(races, ", ", vbCrLf)
    Then if you want to display it back to the page as text, you need to enclose it with <pre></pre> tags to preserve the newlines.
    Code:
    <pre>
    <%=rs("fieldname")%>
    </pre>

  • #4
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I tried exactly this code but don't work. I gets ONLY one 1st value to store in the db field.
    dim races
    races = Request.Form("a")
    races = Replace(races, ", ", vbCrLf)

    I think my question is not so clear.
    On my checkbox values, there are:
    Yes, Asian.
    No, Not Asian
    No, Hot Latino

    Can you help?
    thanks.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by jennypretty View Post

    I think my question is not so clear.
    On my checkbox values, there are:
    Yes, Asian.
    No, Not Asian
    No, Hot Latino

    Can you help?
    thanks.
    Your initial requirement was clear until you said this.
    You mean those are exactly the values of the checkboxes? The values have commas in it? Like this?
    Code:
    value="Yes, Asian"
    If so, then it would be difficult to distinguish the values of each checkbox when they are submitted. The value could be something like this if all are checked.

    Yes, Asian, No, Not Asian, No, Hot Latino

  • #6
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That's right. The values are like that in the table fields. That's why I 'd like to ask if there is a way to to break the each checkbox value in seperate links if multiple checkboxes are selected???

    Thanks.

  • #7
    Regular Coder
    Join Date
    Mar 2007
    Posts
    505
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Have you thought about changing the values of your checkboxes to be integers rather than text?

    Code:
    <input type="checkbox" name="a" id="a" value="1" />Yes, Asian<BR />
    <input type="checkbox" name="a" id="b" value="2" />No, Latino<BR />
    <input type="checkbox" name="a" id="c" value="3" />No, White<BR />
    This would mean A) the code above would work for you, and B) the amount of data stored would be significantly lower. You can even make these dynamic if you wanted to by placing the data into a separate table.

  • #8
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It's a great idea. So, I need to create a new table and join with the main table. And, the values which are stored in the main table
    will be something like this (below) if multiple boxes are selected, am I right?
    ID Race Field
    -- --------
    1 Yes, Asian
    No, White
    2 No, Latino
    3 No, White
    No, Latino

    And, the table structure is like this:
    ID Race
    -- ----
    1 Yes, Asian
    2 No, Latino
    3 No, White

    Thanks.

  • #9
    Regular Coder
    Join Date
    Mar 2007
    Posts
    505
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Sort of --

    The values that would be stored in the main table will only be integers. Only by doing a SELECT...JOIN... would you be able to see the values.

    So the main table example would actually look like this:

    Code:
    ID  Race Field
    --  --------
    1   1, 3
    2   2
    3   3, 2
    If you did the REPLACE() function in your code, then it would look like this:

    Code:
    ID  Race Field
    --  --------
    1   1
        3
    2   2
    3   3
        2


    Your table structure is correct.

    HTH!

  • #10
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Is there any way to see the values directly from the main table?
    Thanks.

  • #11
    Regular Coder
    Join Date
    Mar 2007
    Posts
    505
    Thanks
    1
    Thanked 19 Times in 19 Posts
    When doing a simple select -- no. You would have to do a JOIN statement.

    Code:
    SELECT val1,val2,Race.Name FROM table1 t1 JOIN tableRace Race ON t1.RaceID = t2.RaceID
    That's the only downside to storing only integers. However, you get really good at writing JOIN statements really quickly, too...

    Also, you may run into trouble with the carriage return when pulling values, but that is what Server-Side code, VIEWs, and Queries are for.

    HTH!

  • #12
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I tried to create a new table in ms access like this:
    ID autonumber
    Race TEXT

    ID Race
    -- ----
    1 Yes, Asian
    2 No, Latino
    3 No, White

    And change the Race field on the main table to be Number data type.

    When I run the page and got this error:

    Microsoft JET Database Engine error '80040e07'

    Data type mismatch in criteria expression.

    Should I use all text for fields?

    Thanks.

  • #13
    Regular Coder
    Join Date
    Mar 2007
    Posts
    505
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Your CREATE TABLE is correct, however, because you are going to use commas and/or carriage returns in that field, you need to leave it as TEXT, or set it to a VARCHAR(8).

    Also, make sure that you change your ASP page to do the LOOP for building the INPUT boxes --

    Code:
    <%
    Set rs = Server.CreateObject("ADODB.Recordset")
    Set Conn = Server.CreateObject("ADODB.Connection")
    '
    rs.open "SELECT ID,Name FROM table_Race ORDER BY NAME DESC", conn
      rs.movefirst
        while not rs.eof
          response.write("<input type=""checkbox"" value=""" & rs("ID") & """>" & rs("Name"))
        rs.movenext
      wend
    rs.close
    Conn.close
    '
    Set rs = nothing
    Set conn = nothing
    %>
    To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
    Please, if you found my post helpful, pay it forward. Go and help someone else today.

  • #14
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello,
    I am talking about inserting data in the db table.
    I worked to insert the data. But ONLY the first value was inserted which is "1", even I check 2 boxes.

    Here is my code:

    dim races
    races = Request.Form("a")
    races = Replace(races, ", ", vbCrLf)
    .....

    <input type="checkbox" name="a" id="a" value="1" />Yes, Asian<BR />
    <input type="checkbox" name="a" id="b" value="2" />No, Latino<BR />
    <input type="checkbox" name="a" id="c" value="3" />No, White<BR />

    ....


    Thanks.
    Last edited by jennypretty; 06-04-2007 at 03:58 PM.

  • #15
    Regular Coder
    Join Date
    Mar 2007
    Posts
    505
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Did you set the field back to a TEXT (or VARCHAR) field?

    If you did and you are still getting errors, try:

    Code:
    Response.Write("Form Value: " & request.form("a") & " -- " & races)
    Response.flush
    Let me know what your page replies with and the exact error code, line, etc.
    To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
    Please, if you found my post helpful, pay it forward. Go and help someone else today.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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