How to retrieve values and checked dynamic checkboxes and radio button?

Hi, i am trying to create a page, which enable user to edit their personal profile.
There are checkboxes, which are generated dynamically from database.
then in my database, i will have a table, storing the memid and the checked id.
when user enter this edit personal profile page, i will use the same method of generating this list box, but, how do i assign the value, or values, which have been checked and checked the boxes again?
beside that, i also have radio button which are generated dynamically.
how can i do that?
i am using jsp,
how to i assign let's say, text box value and radio button value?
hope you all guys can help me with this.
really need your help.
iCeFiReAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
cheekycjConnect With a Mentor Commented:
seems like a logic issue try this:
  <%  
        //dynamically generate hobbies list
        int checkedHobb = 100;
        dbHobby.initialize();
        dbMemHobby.initialize();
        dbMemHobby.setHobbyMemId(memId);
        Vector hobbies = dbHobby.getAllHobbies();
        //Get checked list
        Vector memHobb = dbMemHobby.getAllHobbies();
        for (int i = 0; i < hobbies.size(); i++)
        {
             hobbyVO hobbVO = (hobbyVO)hobbies.elementAt(i);
             boolean checked = false;
                  for (int j = 0; j < memHobb.size(); j++)
                  {    
                       memHobbyVO memhobbVO = (memHobbyVO)memHobb.elementAt(j);
                       checkedHobb = memhobbVO.hbHobbyIdVO;
                       if  (hobbVO.hobbyIdVO == checkedHobb)
                         checked = true;
                  }
                  %>
                    <input name="hobbies" type="checkbox" value="<%=hobbVO.hobbyIdVO%>"<% if (checked) { %> checked<% } %>>
                                <span class="texts">
                    <%=hobbVO.hobbyNameVO%><br>
                                <font color="#FFFFFF">...........................</font>
                    </span>
<%     }
       dbMemHobby.getDisconnect();
       dbHobby.getDisconnect();          
                                 
%>

CJ
0
 
Ryan ChongCommented:
Try like:

<input type="checkbox" name="notify1" value="True" <%=(rs.getString("notify1").equals("True"))?"checked":""%>>
0
 
cheekycjCommented:
could you tell us how your database table is layed out (structure wise) in regards to your form.

what cols in the DB relate to the radio button?

assigning values to text boxes are easy:

<input type="text" name="col1" value='<%=rs.getString("col1")%>'>

CJ
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
iCeFiReAuthor Commented:
ok, for my checkboxes, i have 2 fields, one is id, and the name, my checkboxes is for user hobbies.
so once the form load, it will auto generate a list of checkboxes, the id is int, and the name is string.
i will identify the checkboxes by id.
then, from another table. there are 2 fields, one is member id, and one is id.
there may be more than one id for the same member id. i will retrieve the id according to the member id. so let's say, user id of 1001 have 2,5,6
when my form load, i hope that checkboxes, with id 2,5,6 will be checked
0
 
iCeFiReAuthor Commented:
and for the radio button, i know for dynamic one. but if static one?
let's say, i have 3 choices for this radio button. (sorry i dont really know how to call it. hope you all will understand)
then for this 3 choices, let's say is a, b, c.
so from database, i will retrieve the choice user had checked. perhap is b?
then when form load, radio button b will be checked.
Must i use 3 if else to do it?
0
 
cheekycjCommented:
ok.

lets say you have your sql all setup to retrieve the ids

Create a hashtable of all id's with the value defaulted to ""
final static String UNCHECKED = "";
final static String CHECKED = " checked";

java.util.Hashtable allCheckboxes = new java.util.Hashtable();
allCheckboxes.put("1", UNCHECKED);
....
allChecboxes.put("10", UNCHECKED);

Now in your query do this:

while (rs.next()) {
   allCheckBoxes.put(rs.getString("checkbox_id_column"), CHECKED)
}


Now in your form you can do this:
java.util.Enumeration enum = allCheckBoxes.keys();
while (enum.hasMoreElements())
{
  Object checkboxid = enum.nextElement();
  Object checked = allCheckBoxes.get(checkboxid);
  out.println("<input type=checkbox name=" + checkboxid + " value=whatever" + checked + ">");
}


Or another approach would be to store the checked boxes only in a hashtable.

java.util.Hashtable checkedBoxes = new java.util.Hashtable();
while (rs.next()) {
   checkedBoxes.put(rs.getString("checkbox_id_column"), "1")
}

now in your code where you have your form elements do this

<input type="checkbox" name="1" value="whatever" <% if (checkedBoxes.containsKey("1")) { out.print("checked") }%>>

whichever works for you.

CJ
0
 
cheekycjCommented:
as far as the radio buttons go- an

if (..)
else if (...)
else if (...)

should work fine.

CJ
0
 
iCeFiReAuthor Commented:
i tried using this method
     <%  
          //dynamically generate hobbies list
          int checkedHobb = 100;
          dbHobby.initialize();
          dbMemHobby.initialize();
          dbMemHobby.setHobbyMemId(memId);
          Vector hobbies = dbHobby.getAllHobbies();
          for (int i = 0; i < hobbies.size(); i++)
          {
               hobbyVO hobbVO = (hobbyVO)hobbies.elementAt(i);
                   
                    //Get checked list
                    Vector memHobb = dbMemHobby.getAllHobbies();
                    for (int j = 0; j < memHobb.size(); j++)
                    {    
                         memHobbyVO memhobbVO = (memHobbyVO)memHobb.elementAt(j);
                         checkedHobb = memhobbVO.hbHobbyIdVO;
                    }
               if  (hobbVO.hobbyIdVO == checkedHobb)
               {
          %>
                      <input name="hobbies" type="checkbox"                                                      value="<%=hobbVO.hobbyIdVO%>" checked>
                                  <span class="texts">
                      <%out.println(hobbVO.hobbyNameVO); %><br>
                                  <font color="#FFFFFF">...........................</font>
                      </span>
          <%
               }
               else
               {
          %>
                      <input name="hobbies" type="checkbox"                                                      value="<%=hobbVO.hobbyIdVO%>">
                                  <span class="texts">
                      <%out.println(hobbVO.hobbyNameVO); %><br>
                                  <font color="#FFFFFF">...........................</font>
                      </span>
           <%
               }
               }
         dbMemHobby.getDisconnect();
         dbHobby.getDisconnect();          
                                   
     %>
but only the first value will be checked. i put
<input name="hobbies" type="checkbox"                                                      value="<%=hobbVO.hobbyIdVO%>" checked>
                                  <span class="texts">
                      <%out.println(hobbVO.hobbyNameVO); %><br>
                                  <font color="#FFFFFF">...........................</font>
                   
inside the nested loop. all the values will retireve.
but there's the checkbox hobbies value will be repeated.
can anyone teach me how can i solve this?
sorry, cheekycj, wasted your effort, cause what you taught is kind of too difficult for me to understand, so i tried using the easier method.
0
 
cheekycjCommented:
I made some simple changes but I am not following what is wrong:

   <%  
         //dynamically generate hobbies list
         int checkedHobb = 100;
         dbHobby.initialize();
         dbMemHobby.initialize();
         dbMemHobby.setHobbyMemId(memId);
         Vector hobbies = dbHobby.getAllHobbies();
         //Get checked list
         Vector memHobb = dbMemHobby.getAllHobbies();
         for (int i = 0; i < hobbies.size(); i++)
         {
              hobbyVO hobbVO = (hobbyVO)hobbies.elementAt(i);
                   
                   for (int j = 0; j < memHobb.size(); j++)
                   {    
                        memHobbyVO memhobbVO = (memHobbyVO)memHobb.elementAt(j);
                        checkedHobb = memhobbVO.hbHobbyIdVO;
                   }
              if  (hobbVO.hobbyIdVO == checkedHobb)
              {
         %>
                     <input name="hobbies" type="checkbox" value="<%=hobbVO.hobbyIdVO%>" checked>
                                 <span class="texts">
                     <%=hobbVO.hobbyNameVO%><br>
                                 <font color="#FFFFFF">...........................</font>
                     </span>
         <%
              }
              else
              {
         %>
                     <input name="hobbies" type="checkbox" value="<%=hobbVO.hobbyIdVO%>">
                                 <span class="texts">
                     <%=hobbVO.hobbyNameVO%><br>
                                 <font color="#FFFFFF">...........................</font>
                     </span>
          <%
              }
              }
        dbMemHobby.getDisconnect();
        dbHobby.getDisconnect();          
                                   
    %>

what gets repeated is the checked/non check working?
I think there is a problem here:
hobbVO.hobbyIdVO == checkedHobb

what is hobbyIDVO ? is it a string? an int or what?

CJ
0
 
iCeFiReAuthor Commented:
HobbyIdVO is an int
the checked working. let's say, in my memhobbVO, i have 2 values. and i have 6 for hobbies too.
and for my hobbies, i have entertainment, pc, game, sport, book, shopping for example, they are my checkboxes values.
so it will look something lk
entertainment
pc
game
sport
book
shopping

in my html page.
so in my memhobbVo, perhap book and sport is to be checked
when i do that, it will result in
pc
pc
game
game
sport(checked)
sport(checked)
book(checked)
book(checked)
shopping
shopping
0
 
iCeFiReAuthor Commented:
but for the code you helped me with
now is like
pc
game
sport
book(checked)
shopping
only the first one will be checked
0
 
iCeFiReAuthor Commented:
...........................  Entertainment(checked)
...........................  Entertainment
...........................  Entertainment
...........................  Entertainment
...........................  Entertainment
...........................  Home & Family
...........................  Home & Family
...........................  Home & Family
...........................  Home & Family
...........................  Home & Family
...........................  Health & Beauty
...........................  Health & Beauty
...........................  Health & Beauty
...........................  Health & Beauty
...........................  Health & Beauty
...........................  Music
...........................  Music(checked)
...........................  Music
...........................  Music
...........................  Music
...........................  Games
...........................  Games
...........................  Games(checked)
...........................  Games
...........................  Games
...........................  Computers & Technology
...........................  Computers & Technology
...........................  Computers & Technology
...........................  Computers & Technology
...........................  Computers & Technology
...........................  Shopping
...........................  Shopping
...........................  Shopping
...........................  Shopping(checked)
...........................  Shopping
...........................  Sports & Outdoors
...........................  Sports & Outdoors
...........................  Sports & Outdoors
...........................  Sports & Outdoors
...........................  Sports & Outdoors
...........................  Travel
...........................  Travel
...........................  Travel
...........................  Travel
...........................  Travel(checked)
...........................  Other
...........................  Other
...........................  Other
...........................  Other
...........................  Other

this is something my page look like now.
after i put the if else condition inside the nested loop
sorry to be so naggy

0
 
iCeFiReAuthor Commented:
It works!
thanks alot!
0
 
cheekycjCommented:
Glad I could help and Thanx for the "A"

CJ
0
All Courses

From novice to tech pro — start learning today.