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]

[3930 byte] By [yupngoclam@yahoo.com.sga] at [2007-10-2 5:48:32]
# 1

The definition

"<input type="radio" name="button1" value="ON" checked>ON<input type="radio" name="button2" value="OFF">" itself is wrong.

name="button1" here is the group name. so u have to use same group name in both the cases, and make a check in the while loop

<input type="radio" name="button1" value="ON" checked>ON<input type="radio" name="button1" value="OFF">

Innovationneverstopsa at 2007-7-16 1:58:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Try it:

<% String check1 = "";

String check2="";

if(status.equals("on")) check1="checked";

if(status.equals("off")) check2="checked";

%>

<input type="radio" name="button1" value="ON" ><%=check1%>>ON<input type="radio" name="button1" value="OFF" ><%=chech2%>>OFF

Rahul.Guptaa at 2007-7-16 1:58:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
what you can do is Use a bean.with get and Set function.first Set the value .and then in ur form in the radio Button Tag you can use beanname.getcheckstatus("On"). which will return checked if the value set is "On".I hope this works for you
umesh1da at 2007-7-16 1:58:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

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>

<%

}

%>

yupngoclam@yahoo.com.sga at 2007-7-16 1:58:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

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>

Sunil_chda at 2007-7-16 1:58:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
After value="ON" put single opening angle bracket and same after value="OFF" .. it is getting automatically closed while posting ..
Sunil_chda at 2007-7-16 1:58:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
hi Sunil_chd, Thankx for your help. I tried that method, but still can not work. The buttons are blank. Does anyone know what's wrong with that codes? Plz help me
yupngoclam@yahoo.com.sga at 2007-7-16 1:58:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

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>

<%

}

%>

Roridgea at 2007-7-16 1:58:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
actually dont do that... for some reason the forum is putting in an extra ">" after the "ON" and "OFF" text.I will email you the code
Roridgea at 2007-7-16 1:58:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
Thank you so much, Rorigde and all of you guys here. Finally my page is working now
yupngoclam@yahoo.com.sga at 2007-7-16 1:58:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...