Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: See if checkbox is checked

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default See if checkbox is checked

    The page below will display 2 checkboxes with the values for user with ID 1.

    Movies
    Series

    Now I have some values in my DB that make sure both of these boxes are checked.

    TABLE 'type'
    -----------
    typeID        typeName
    1                Movies
    2                Series
     
    TABLE 'user'
    ------------
    userID        userName        password
    1               admin             admin
    2               test               test
     
    TABLE 'usersType'
    usersTypeID        userID        typeID        checked
    1                        1                1                yes
    2                        1                2                yes
    3                        2                1                yes

    Below is the code for the preferences.jsp page

    <%@page contentType="text/html; charset=iso-8859-1" language="java"%>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %>
     
    <!DOCTYPE html>
    <html>
        <head>
            <title>Preference Page</title>
        </head>
        <body bgcolor="#0099CC">
            <div id="navigation">
                <ul class="top-level">
                    <li><a href="success.jsp">Home</a></li>
                    <li><a href="preferences.jsp">Preferences</a></li>
                    <li><a href="recommendations.jsp">Recommendations</a></li>
                </ul>
            </div>
            <div id="wrapper">
                <%
                     Connection connection = null;
                     Statement statement = null;
                     ResultSet rs = null;
                     Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                     connection = DriverManager.getConnection("jdbc:derby://localhost:1527/recommendation;create=true;user=root;password=root");
                     statement = connection.createStatement();                 
                %>
                <form name="frmPreferences" action="updateDatabase.jsp" method="post">
                    <table>
                        <tr>
                            <td>Do you like to watch movies or series? Or both?</td>
                        </tr>
                        <%
                            String QueryString = "SELECT * FROM APP.TYPE LEFT JOIN APP.userstype ON APP.TYPE.typeID = APP.userstype.typeID AND APP.userstype.userID = " + session.getAttribute("ID");
                            rs = statement.executeQuery(QueryString);
     
                            while(rs.next()){
                                String checked = rs.getString(6);
                        %>
                        <tr>
                            <td><input type="checkbox" name="type"<% if(checked == null){ checked = ""; } if(checked.equals("yes")){ out.print(" checked='checked'"); }else{ out.print("");}%> value="<%=rs.getInt(1)%>" /><%=rs.getString(2)%></td>
                        </tr>
                        <%
                            }
                            rs.close();
                        %>
                    </table>
                </form>
                        <%
                            // close all connections
                            statement.close();
                            connection.close();
                        %>
                <br /><br />
                <a href="logout.jsp"><b>Logout</b></a>
            </div>
        </body>
    </html>

    Now, I want to update these tables via this page. But I don't know how to see if the checkbox is really checked or not.

    <%@page contentType="text/html; charset=iso-8859-1" language="java"%>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %>
     
    <%
         Connection connection = null;
         PreparedStatement ps = null;
         Statement statement = null;
         ResultSet rs = null;
         Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
         connection = DriverManager.getConnection("jdbc:derby://localhost:1527/recommendation;create=true;user=root;password=root");
     
         /*statement = connection.createStatement();
         String QueryString = " ";
         rs = statement.executeQuery(QueryString);*/
     
         // get the checkbox values
         String c[]= request.getParameterValues("type");
     
        // I'M KINDA STUCK HERE...
    %>


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: See if checkbox is checked

    If I understand your question correctly, the API is your friend: Java Platform SE 6

    Hint: You're looking for a method that tells you whether the JCheckBox is selected, right?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: See if checkbox is checked

    if you read my post you'll see i want to see if an html checkbox is checked

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: See if checkbox is checked

    Quote Originally Posted by kney View Post
    if you read my post you'll see i want to see if an html checkbox is checked
    How silly of me to assume a question posted on a Java forum was related to Java. Anyway, much luck to you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: See if checkbox is checked

    lol don't even bother then..
    there isn't any java code to read an html checkbox?
    cuz that's what i need

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: See if checkbox is checked

    I guess I'm a bit confused. If you're working in JSP, won't the value of the checkbox selection be in the request?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: See if checkbox is checked

    Hi kney,

    This is exactly what you need: checkbox value to servlet (Servlets forum at JavaRanch)

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 04:01 PM.

  8. #8
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: See if checkbox is checked

    @KevinWorkman

    I'll explain it a bit further
    Like I have it now, my checkboxes are checked because of the values in the DB.
    If I deselect the second checkbox my database should change to

    TABLE 'usersType'
    usersTypeID        userID        typeID        checked
    1                        1                1                yes
    2                        1                2                no
    3                        2                1                yes

    But since I can only get the value of a selected checkbox, how can I see if a checkbox is actually selected or not?
    If I deselect the 2nd checkbox I only get value "1" from this code.

    // get the checkbox values
    String c[]= request.getParameterValues("type");

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: See if checkbox is checked

    If I deselect the 2nd checkbox I only get value "1" from this code.
    The reason might be, that you have created all the check boxes on your webpage dynamically with same name. So, if there are three check boxes of the same name, it doesn't know which one is checked and which not. Also, in java you can check if the checkbox is selected or not.
    It's not Java's fault but actually your code's or logic's fault.

  10. #10
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: See if checkbox is checked

    that's exactly what I did. lol
    well.. my logic isn't logical :p

Similar Threads

  1. Replies: 1
    Last Post: September 21st, 2011, 01:05 AM
  2. checkbox problem in jsp
    By kundan_101 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 17th, 2011, 08:12 AM
  3. retain checked status of checkbox
    By rahulj5 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 29th, 2010, 12:10 AM
  4. Checking the Status of a checkbox
    By michaelz in forum Object Oriented Programming
    Replies: 16
    Last Post: August 9th, 2010, 06:44 AM
  5. how to delete record based on checkbox selected checkbox
    By -_- in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: December 15th, 2009, 09:26 PM