1 Attachment(s)
I have a problem in my JSP code
I have this code and I am displaying contents from database in the browser.In database I have questions for survey with options as answers.For each question users have to select any one of the four options.I am using radio buttons for getting user response.I am showing the output that I am getting
Attachment 682
The problem is that I want that users should be able to select only one option out of the 4 options for a question but this code is making me able to select all the four options for a question.Conversely it is allowing me select only one option for a column while it should allow me to select multiple.Like if you look at the first column of the options(3rd column from left) than it allows me select only one option throughout the column.I want to have this on the row and not column.Please can anyone help me with this?
Code :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>display</title>
</head>
<body>
<h2>Data</h2>
<%
try {
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
System.out.println("loading class") ;
Class.forName("com.ibm.db2.jcc.DB2Driver");
System.out.println("after loading class");
connection = DriverManager.getConnection("jdbc:db2://localhost:50000/Simple","administrator","welcome2sa2");
System.out.println("after conncetion class");
System.out.println(connection.getClass());
statement = connection.createStatement();
String QueryString = "select * from question";
rs = statement.executeQuery(QueryString);
%>
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString(1)%></TD>
<TD><%=rs.getString(2)%></TD>
<TD><input type='radio'name='radio1'><%=rs.getString(3)%></TD>
<TD><input type='radio'name='radio2'><%=rs.getString(4)%></TD>
<TD><input type='radio'name='radio3'><%=rs.getString(5)%></TD>
<TD><input type='radio'name='radio4'><%=rs.getString(6)%></TD>
</TR>
<% } %>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
%>
<font size="+3" color="red"></font>
<%
out.println("Unable to connect to database.");
}
%>
</TABLE>
</body>
</html>
Re: I have a problem in my JSP code
Is this a question about what HTML code to generate to get Radio buttons?
Re: I have a problem in my JSP code
Your loop makes 4 radio controls on a table row, with names coded from 1 to 4. Then it makes another row, using the same names. Radio controls with the same name run through columns of your HTML, not through table rows. You would be able to see that clearly if you posted the HTML output to the User-agent.
Is that a Java Server Page? Nasty. It's like a combination of the worst parts of Java and PHP.
Re: I have a problem in my JSP code
Quote:
Originally Posted by
Sean4u
Your loop makes 4 radio controls on a table row, with names coded from 1 to 4. Then it makes another row, using the same names. Radio controls with the same name run through columns of your HTML, not through table rows. You would be able to see that clearly if you posted the HTML output to the User-agent.
Is that a Java Server Page? Nasty. It's like a combination of the worst parts of Java and PHP.
That even I know why the problem is coming.I want to know how to correct it.I want to allow users to select one option from each row and not column.If you could correct my code for doing that it would be great
Re: I have a problem in my JSP code
Can you create the HTML code you want with an editor so you know what needs to be generated
and then change the jsp code to generate that desired HTML.
Re: I have a problem in my JSP code
Quote:
That even I know why the problem is coming
I'm at a loss to understand why you haven't fixed it in that case.
You've got this:
Code :
<tr>1 2 3 4</tr>
<tr>1 2 3 4</tr>
<tr>1 2 3 4</tr>
<tr>1 2 3 4</tr>
output a row at a time by a while loop and you want this:
Code :
<tr>1 1 1 1</tr>
<tr>2 2 2 2</tr>
<tr>3 3 3 3</tr>
<tr>4 4 4 4</tr>
Have a go at editing that loop yourself to get the numbers in the right place, and if it still doesn't work, post your latest code again.
Re: I have a problem in my JSP code
Quote:
Originally Posted by
Sean4u
I'm at a loss to understand why you haven't fixed it in that case.
You've got this:
Code :
<tr>1 2 3 4</tr>
<tr>1 2 3 4</tr>
<tr>1 2 3 4</tr>
<tr>1 2 3 4</tr>
output a row at a time by a while loop and you want this:
Code :
<tr>1 1 1 1</tr>
<tr>2 2 2 2</tr>
<tr>3 3 3 3</tr>
<tr>4 4 4 4</tr>
Have a go at editing that loop yourself to get the numbers in the right place, and if it still doesn't work, post your latest code again.
This is my latest code snippet.I am only posting the while loop because prob is in that only.I have taken a variable int i and I am using it to name the radio buttons.But now it is only allowing me to select one option throughout the grid(means in all rows and columns).I think it is naming all the radio buttons with the same name.And there is one more thing.The number of rows in the table are not fixed.Even though the image is showing only 4 rows but in reality they can be of any number.Right now I am trying for 4 rows only.When the prob is solved for 4 rows it can be converted to any number of rows by getting the number of rows from database and storing it in i.Can you correct the code.
Code :
<%int i=0; %>
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
<%
while (rs.next()&&(i<4)) {
%>
<TR>
<TD><%=rs.getString(1)%></TD>
<TD><%=rs.getString(2)%></TD>
<TD><input type='radio' name='radio(i+1)'><%=rs.getString(3)%></TD>
<TD><input type='radio' name='radio(i+1)'><%=rs.getString(4)%></TD>
<TD><input type='radio' name='radio(i+1)'><%=rs.getString(5)%></TD>
<TD><input type='radio' name='radio(i+1)'><%=rs.getString(6)%></TD>
</TR>
<%i++; %>
<% } %>
Re: I have a problem in my JSP code
Quote:
Originally Posted by
Norm
Can you create the HTML code you want with an editor so you know what needs to be generated
and then change the jsp code to generate that desired HTML.
I am trying to do that only but every time I am getting logical error.I am not able to figure out the correct way.My latest code is posted above.
Re: I have a problem in my JSP code
That's more like it, though you should have included your declaration of 'i' in that code fragment. What you should really be doing here is changing your JSP, loading the page in your browser to test it, and when it doesn't work the way you expect, you should be looking at the HTML source of the page to see what your JSP has output. View your HTML source in your browser now - it's usually right-click somewhere on the page and 'View Source' or similar. Select, copy and paste the whole <table></table> that contains your radio controls. Your current JSP code looks to me like it should give you the right output, so we need to double-check the HTML source to see why it isn't working.