
I have a small doubt in my application, my requirement is to get a "single selectible row", I generated a radio button for each row as i got data from the database using iterator tag,but when i select any row the it is going to bind the last iterated value, please can anyone clear my doubt.and disabled all previous values.
myjsp is like that
<%@ taglib prefix="display" uri="http://displaytag.sf.net/el"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@page import="java.util.*"%>
<%@page import="java.util.Iterator"%>
<%@page import="inc.eis.info.api.TestQuestion"%>
<h3 align="center">List of Questions</h3>
<table width="530" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><table width="530" border="0" align="center" cellpadding="0" cellspacing="0" >
<tr>
<td align="left" valign="top" style="padding:6px;" class="borderred">
<table border="0" align="left" cellpadding="0" cellspacing="0" width="95%">
<s:form theme="simple" name="startTestForm" >
<!-- <hr width="100%" bordercolor="red"> -->
<table>
<%! HashMap s=null; %>
<%
List qs=(List)request.getAttribute("list");
Iterator questions= qs.iterator();
for(int i=1;i<2;i++){
TestQuestion tqDet=(TestQuestion)qs.get(i);
s=new HashMap();
s.put(i,tqDet);
%>
<tr>
<td colspan="2" align="left"><b class="textboldblack"><%out.println(i);%><%out.println(tqDet.getQuestions());%></b></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="test" id="A" ><%out.println(tqDet.getAns_a());%></b></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="test" id="B"><%out.println(tqDet.getAns_b());%></b></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="test" id="C"><%out.println(tqDet.getAns_c());%></b></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="test" id="D"><%out.println(tqDet.getAns_d());%></b></td></tr>
<%
}
%>
<tr><td colspan="2" align="center"><input type="submit" value="Next" onclick="callAction()"></td>
</tr>
</table>
</s:form>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<%-- <s:hidden name="" id="radioButtons[x].id" value="seloption"></s:hidden> --%>
<script language="javascript">
function callAction(){
alert("hi ");
var radioButtons = document.getElementsByName("test");
for (var x = 0; x < radioButtons.length; x ++) {
if (radioButtons[x].checked) {
var option=radioButtons[x].id;
// var seloption=document.getElementById("radioButtons[x].id").value;
alert("your selected answer is:: " +option);
document.startTestForm.action="radioSave.action?option="+option;
document.startTestForm.submit();
//alert("You checked " + radioButtons[x].id + " which has the value " + radioButtons[x].value);
}
}
}
</script>

We are providing you a simple application.
Create table test(ques,op1,op2,op3,op4,ans) and answers(id,ans). Then try the following code:
1)form.jsp:
<%@page import="java.sql.*"%>
<html>
<form method="post" action="result.jsp">
<table>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root";, "root");
Statement st=connection.createStatement();
ResultSet rs=st.executeQuery("Select * from test");
int i=1;
while(rs.next()){
%>
<tr><td><%=i%></td><td><%=rs.getString("ques")%></td><td><input type="radio" value="<%=rs.getString("op1")%>" name="radio<%=i%>"/><%=rs.getString("op1")%></td><td><input type="radio" value="<%=rs.getString("op2")%>" name="radio<%=i%>"/><%=rs.getString("op2")%></td><td><input type="radio" value="<%=rs.getString("op3")%>" name="radio<%=i%>"/><%=rs.getString("op3")%></td><td><input type="radio" value="<%=rs.getString("op4")%>" name="radio<%=i%>"/><%=rs.getString("op4")%></td></tr>
<%
i++;
}
%>
<tr><td><input type="submit" value="submit"></td></tr>
</table>
</form>
</html>
2)result.jsp:
<%@page import="java.sql.*"%>
<%
String st[]=new String[10];
for(int i=0;i<st.length;i++){
int j=i+1;
st[i]=request.getParameter("radio"+j);
System.out.println(st[i]);
}
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root";, "root");
Statement stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery("Select ans from answers");
String ans="";
while(rs.next()){
ans+=rs.getString("ans")+" ";
}
int count=0;
String answers[]=ans.split(" ");
for(int i=0;i<answers.length;i++){
if(st[i].equals(answers[i])){
count++;
}
}
out.println("Your "+count+" answers are correct");
%>

thanks deepak . i got the soution.

Hi deepak,
how to get the multiple selected radio button values from jsp to action using javascript.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.