How to retrieve and input data to option button?
[nobr]hi, I want to set my option button ON or OFF checked according to the status that i retrieve from my database.Example in this case, I want to retrieve the status of light from my database. If status is ON, it will check the On option button, else it will check my Off option button. Can anyone help me Please?
<%@ page import="java.sql.*" %>
<jsp:useBean id="dbselect" class="database.dbSelect" scope="session"/>
<jsp:useBean id="cart" scope="session" class="database.ShoppingCart" />
<!-- Add an HTML table to format the results --><style type="text/css">
<!--
body{
background-color: #000000;
}
body,td,th{
color: #FFFFFF;
}
.style2{color: #CCCCCC}
.style3{color: #FF00FF}
.style4{
color: #66CCFF;
font-weight: bold;
}
-->
</style>
Welcome, <br>
<center>
<h1 class="style3">Home Controller</h1><br>
Today is <%=new java.util.Date()%>
</center>
<frame>
<TABLE BORDER="1" width ="400" cellspacing="0" cellpadding ="2" align ="center">
<TR>
<TH>Location</TH>
<TH>Device</TH>
<TH>Status</TH>
</TR>
<%
String query ="SELECT * FROM Controller";
dbselect.rs = dbselect.executeQuery(query);
while ( (dbselect.rs).next() ){
String device = dbselect.rs.getString("Device");
String location= dbselect.rs.getString("Location");
String status = dbselect.rs.getString("Status");
%>
<TR><form action="MyController.jsp" method="post">
<TD><%= location %></TD>
<TD><%= device %> </TD>
<TD> <input type="hidden" name="status" value="<%= status %>" >
<input type="radio" name="button1" value="ON" checked>ON <input type="radio" name="button2" value="OFF">OFF</TD>
</form>
</TR>
<%
}
%>
</TABLE>
<center><input type="submit" name="ChangeStatus" value="Save Change"> <input type="Reset" name="Reset" value="Don't Save"> </center>
</frame>
<br>
<hr color=#a0cfeb>
<br>
<center>
<span class="style2">Copyright </span><span class="style4">NgocLam@np.edu.sg</span> 2005
</center>
[/nobr]
hey guys, thanks for your helping. BTW, plz take a look at this below codes. I am still trying to troubleshoot this problem
<%
String query = "SELECT * FROM Controller";
dbselect.rs = dbselect.executeQuery(query);
while ( (dbselect.rs).next() ) {
String device = dbselect.rs.getString("Device");
String location= dbselect.rs.getString("Location");
String status = dbselect.rs.getString("Status");
String check1 = "";
String check2="";
if(status.equals("ON"))
{
check1="checked";
check2="unchecked";
}
if (status.equals("OFF"))
{
check2="checked";
check1="uncheked";
}
%>
<TR><form action="MyController.jsp" method="post">
<TD><%= location %></TD>
<TD><%= device %> </TD>
<TD><input type="radio" name="button1" value="ON" "><%=check1%>"> ON <input type="radio" name="button1" value="OFF" "><%=check2%>" >OFF</TD>
</form>
</TR>
<%
}
%>
try this its working fine !!
String device = dbselect.rs.getString("Device");
String location= dbselect.rs.getString("Location");
String status = dbselect.rs.getString("Status");
String check1 = "";
String check2="";
if(status.equals("ON"))
{
check1="checked";
check2="unchecked";
}
if (status.equals("OFF"))
{
check2="checked";
check1="uncheked";
}
%>
<table>
<TR><form action="#" method="post">
<TD><%= location %></TD>
<TD><%= device %> </TD>
<TD><input type="radio" name="button1" value="ON" ><%=check1%>> ON<input type="radio" name="button1" value="OFF" ><%=check2%> >OFF</TD>
</form>
</TR>
</table>
try this, copy and paste this code directly in the place of your code.
<%
String query = "SELECT * FROM Controller";
dbselect.rs = dbselect.executeQuery(query);
while ( (dbselect.rs).next() ) {
String device = dbselect.rs.getString("Device");
String location= dbselect.rs.getString("Location");
String status = dbselect.rs.getString("Status");
String check1 = "";
String check2="";
if(status.equalsIgnoreCase("ON")
{
check1="checked";
}
if (status.equalsequalsIngoreCase("OFF"))
{
check2="checked";
}
%>
<TR><form action="MyController.jsp" method="post">
<TD><%= location %></TD>
<TD><%= device %> </TD>
<TD><input type="radio" name="button1" value="ON" ><%=check1%> > ON<input type="radio" name="button1" value="OFF" ><%=check2%> >OFF</TD>
</form>
</TR>
<%
}
%>