?
Solved

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

Posted on 2003-03-24
14
Medium Priority
?
7,355 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:iCeFiRe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 8202193
Try like:

<input type="checkbox" name="notify1" value="True" <%=(rs.getString("notify1").equals("True"))?"checked":""%>>
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8202852
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
 

Author Comment

by:iCeFiRe
ID: 8202948
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:iCeFiRe
ID: 8202967
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 8203134
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 8203141
as far as the radio buttons go- an

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

should work fine.

CJ
0
 

Author Comment

by:iCeFiRe
ID: 8208971
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 8211163
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
 

Author Comment

by:iCeFiRe
ID: 8214736
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
 

Author Comment

by:iCeFiRe
ID: 8214749
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
 

Author Comment

by:iCeFiRe
ID: 8214765
...........................  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
 
LVL 19

Accepted Solution

by:
cheekycj earned 200 total points
ID: 8219057
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
 

Author Comment

by:iCeFiRe
ID: 8221888
It works!
thanks alot!
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8224314
Glad I could help and Thanx for the "A"

CJ
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question