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.
Code :
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
Code :
<%@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.
Code :
<%@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...
%>
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?
Re: See if checkbox is checked
if you read my post you'll see i want to see if an html checkbox is checked
Re: See if checkbox is checked
Quote:
Originally Posted by
kney
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.
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
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?
Re: See if checkbox is checked
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
Code :
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.
Code :
// get the checkbox values
String c[]= request.getParameterValues("type");
Re: See if checkbox is checked
Quote:
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.
Re: See if checkbox is checked
that's exactly what I did. lol
well.. my logic isn't logical :p