How to set checked Checkbox based on database record?

无人久伴 提交于 2019-12-24 13:26:08

问题


I would like to raise a question about JSP checkbox

I want to loop the subscription list from the database.

At the same time, I loop the specific user's subscription request.

For example, in the subscription list, there are several items, says

System Analyst
System Developer
System Manager
System Tester

In the subscription list, the user subscribed 2 of them, says

System Manager
System Tester

In the looping, I will loop a table with a checkbox column to show all the subscription value.

I encounter a problem here.

I want to make the checkbox "checked" while the user has subscribed the item.

For example, the result of table should have "checked" 2 of the checkbox.

However, I don't know the algorithm to implement my logic.

Can anyone kindly provide some hints for me to realize this?

Thanks.

siris

Coding-

<tr>
    <td>Subscription:</td>
    <td>
    <select name="subscription"> 

//read the subscription List
ResultSet rs=statement.executeQuery("select * from subsrciption");

//read the user has subscipted what item(s)
ResultSet rs2=statement.executeQuery("select * from request where userid='testing' ");
//in this SQL, for userid-testing, we find 2 records that he subscribed 2 items

//loop the subscription list from database
while ( rs.next() ) {
  String subscription = rs.getString("subscription");
  //after read the subscription table, how to loop the request table
  //while ( rs2.next() ) {

%>
  <option value="<%=subscription%>"><%=subscription%></option>
<%
  //} 
} //end loop subscription list

%>
    </select>
    </td>
 </tr>

回答1:


You have asked about checkbox in the question and writing for Select in code.

Please edit your post. Use

<INPUT TYPE=CHECKBOX NAME="<%=subscription%>"><%=subscription%>

Secondly,

Get subscription value from rs2 and compare it with rs



来源:https://stackoverflow.com/questions/5052003/how-to-set-checked-checkbox-based-on-database-record

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!