Link to home
Start Free TrialLog in
Avatar of ScottyMac
ScottyMac

asked on

Servlets: Saving a Checkbox value

I'm developing a servlet that either displays info or saves info, depending on the flag passed.  When displaying the info from the Access2000 db, I have a checkbox displaying the value from the "approved" column in my db.  In the db, if the value is 'Yes', the box is checked, and if 'No', then it's not.  This works for me fine.  

But, I would like the user to be able to check/uncheck the "approved" checkbox on the html page that the servlet developed and then be able to save whatever changes she made in the db.


Here's how I enable the checkbox to be checked/unchecked from the db:

String approved = "";
String myyes = "Yes";

approved = rs.getString("approved");
if (approved.equals(myyes))
{
    out.println("<td width=\"98\"><input type=\"checkbox\" name=\"chkapproved\" value=\"ON\" checked></td>");
                              }
                              else
                              {
                                    out.println("<td width=\"98\"><input type=\"checkbox\" name=\"chkapproved\" value=\"OFF\"></td>");
                              }
                        
                        

I hope this is clear!

scottymac
Avatar of ScottyMac
ScottyMac

ASKER

Adjusted points to 100
ASKER CERTIFIED SOLUTION
Avatar of Ravindra76
Ravindra76

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So the srevlet now displays the info from the db.
For you to get the info from the html page & store it in db,
1. In the form action of the html page invoke a servlet.
2. This servlet can acess the form objects thru getParameter() & then store this in the database.

in which part do u require assistance?
-sgoms
Hi ScottyMac,

Use a parameter ( Assume name is fromUser )

out.println("<input type=hidden name=fromUser value=yes>"); in your code in displaying form to user

service() method code now is

String fromUser = req.getParameter("fromUser");

if ( fromUser.equalsIgnoreCase("yes")) {
 
  //write my code in this function

}
else
{  
   // put your code in this function

}

Best of luck
I figured it out, I appended the ID of the each record to the name of the checkbox.  
ex.

if (approved.equals(myyes))
{
out.println("<td width=\"98\"><input type=\"checkbox\" name=\"chkapproved(" + myid + ")\" value=\"YES\" checked></td>");
                                    }
                                    else
                                    {
      out.println("<td width=\"98\"><input type=\"checkbox\" name=\"chkapproved(" + myid + ")\" value=\"NO\"></td>");
                                    }
 
So, when I posted the values, I could regenerate the same record set and check the values by using "req.getParameter("chkapproved(" + myid + ")");

And then I was able to get the values of each individual checkbox.

there was some quirkiness with the Statement object, I had to use two different instances, but it works now....the thing I found out about checkboxes is that if they are checked, they pass the value when posted, but if they aren't checked, they pass a null value......therefore, you can just check for "un-checked" value by seeing if there was a null value passed.

                       
ResultSet rs = myStatement.executeQuery("Select * from bulletin_board"); // ORDER BY date_posted DESC");
                       
out.println("<HTML>");
out.println("<HEAD><TITLE>Bulletin Board</TITLE><HEAD>");
out.println("<BODY>");
int myid;      
String myapproved = "";
Statement yourStatement = con.createStatement();
                       
while (rs.next())
{
myid = rs.getInt("bulletin_ID");
myapproved = req.getParameter("chkapproved(" + myid + ")");
if (myapproved == null)
                                    {out.println("<p><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\">Bulletin_id = " + myid + " ChkApproved = 'NO'</font></p>");
                                         yourStatement.executeUpdate("UPDATE bulletin_board SET approved = 'NO' WHERE bulletin_ID = " + myid + "");                  
                                    }
                                    else
                                    {
yourStatement.executeUpdate("UPDATE bulletin_board SET approved = 'YES' WHERE bulletin_ID = " + myid + "");            
                             out.println("<p><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\">Bulletin_id = " + myid + " ChkApproved = 'YES'</font></p>");
      }
      
}
                            out.println("</BODY></HTML>");
            
//Close the ResultSet
rs.close();


Thanks,

scottymac