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
ScottyMacAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ScottyMacAuthor Commented:
Adjusted points to 100
0
Ravindra76Commented:
Hi ScottMac,

 Heat your problem man?

 I will tell logic from Html page user cliecs to servlet saving db.

 String confirmedFromHtmlPage = req.getParameter("chkapproved");


if ( confirmedFromHtmlPage != null)
if ( confirmedFromHtmlPage .equalsIgnoreCase("On")) {

 myyes = "Yes";

}else
{
 myyes = "No";
}

// Write Sql statements to save into //database
// Your Routing code

Best of luck
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sgomsCommented:
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
0
Ravindra76Commented:
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
0
ScottyMacAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.