[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

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
0
ScottyMac
Asked:
ScottyMac
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now