Connecting Tech Pros Worldwide Forums | Help | Site Map

how to sort the table using combo box values

Newbie
 
Join Date: Jul 2007
Posts: 10
#1: Jul 6 '07
hi...

I want to sort the table using combobox values. I give the code here.

address.html:

<html>
<head>
<title>Add a new entry</title>
</head>
<body>
<h1>Please enter the new address information</h1>
<form method="POST" action="datajsp.jsp">
Name: <input type="text" name="name" size="20"><br>
Street: <input type="text" name="street" size="20"><br>
City: <input type="text" name="city" size="20"><br>

Country: <input type="text" name="country" size="20"><br>
Telephone: <input type="text" name="tel" size="20">
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>


datajsp.jsp:


<%@page import="java.sql.*"%>
<html>
<head>
<title>Add a new Address</title>


<script type="text/javascript">

function sort()
{
var k=document.getElementById("mylist").value;
alert(k);
}
</script>



</head>
<body>
<form name=f1 action="http://localhost:8080/examples/datadisplay.jsp">
<h1>New Address Creation using executeUpdate()</h1>
<%
Connection conn = null;
PreparedStatement stmt = null;
try {
Class c = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (Exception e) {
System.out.println("Error occurred " + e);
}
try {
conn = DriverManager.getConnection("jdbc:odbc:data");

}
catch (Exception e) {
System.out.println("Error occurred " + e);
}
try {
stmt = conn.prepareStatement("INSERT INTO address(name, street, city, country, telephone) VALUES (?, ?, ?, ?, ?)");
stmt.setString(1, request.getParameter("name"));
stmt.setString(2, request.getParameter("street"));
stmt.setString(3, request.getParameter("city"));

stmt.setString(4, request.getParameter("country"));
stmt.setString(5, request.getParameter("tel"));
stmt.executeUpdate();
stmt.close();
conn.close();
}
catch (Exception e) {
System.out.println("Error occurred " + e);
}
finally {
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {}
try {
if (conn != null)
conn.close();
} catch (Exception e) {}
}

%>
The new address has been created.
<br><br>


<select id="mylist">
<option value="name" >name</option>
<option value="street">street</option>
<option value="city">city</option>
<option value="country">country</option>
<option value="telephone">telephone</option></select>
<br><br><font style="margin-left:100px; "></font>
<input type="submit" value="display" onClick="sort();">
</form>
</body>
</html>

datadisplay.jsp:



<%String s="";
s=request.getParameter("k");%>
<%@ page import = "java.sql.*"%>
<html>
<body>
<%
Connection connection = DriverManager.getConnection(
"jdbc:odbc:data", "", "");

Statement statement = connection.createStatement();

String name=request.getParameter("name");
String street=request.getParameter("street");
String city=request.getParameter("city");
String country=request.getParameter("country");
String telephone=request.getParameter("telephone");
ResultSet rs=statement.executeQuery("select * from address order by '"+s+"'");

%>
<TABLE BORDER="1">
<TR>

<TH>Name</TH>
<TH>Street</TH><TH>City</TH><TH>Country</TH><TH>TelePhone</TH>

</TR>
<%
try
{
while(rs.next()){


%>
<TR>
<TD> <%= rs.getString(1) %></td>
<TD> <%= rs.getString(2) %></TD>
<TD> <%= rs.getString(3) %></TD>
<TD> <%= rs.getString(4) %></TD>
<TD> <%= rs.getString(5) %></TD>
</TR>
<% }

}
catch(Exception e)
{
System.out.println(e);
}
%>
</TABLE>
</body>
</html>

In this the table displayed correctly.but i select one value from the combobox and click the display button but it was not the table is sorted.

this is very urgent for me. Please anyone can help me..

Thanks in advance.

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jul 6 '07

re: how to sort the table using combo box values


1.)Please use code tags everytime when posting code.
Quote:

Originally Posted by Nirmala123

function sort()
{
var k=document.getElementById("mylist").value;
alert(k);
}
</script>.

Is this all the sort function that you have? Surely you don't expect that the sort the table.
Newbie
 
Join Date: Jul 2007
Posts: 10
#3: Jul 6 '07

re: how to sort the table using combo box values


Quote:

Originally Posted by r035198x

1.)Please use code tags everytime when posting code.

Is this all the sort function that you have? Surely you don't expect that the sort the table.

Thanks for your reply...

no i want to get that k value in next page. For all the sorting i do in the database query.

so please help me how to get that combobox value(that mean k in sort()) in the query.

ResultSet rs=statement.executeQuery("select * from address order by '"+s+"'");

Thanks in Advance.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jul 6 '07

re: how to sort the table using combo box values


Quote:

Originally Posted by Nirmala123

Thanks for your reply...

no i want to get that k value in next page. For all the sorting i do in the database query.

so please help me how to get that combobox value(that mean k in sort()) in the query.

ResultSet rs=statement.executeQuery("select * from address order by '"+s+"'");

Thanks in Advance.

Put the value in a hidden input field on the form that's submitted. Then in the next page you get that value using

Expand|Select|Wrap|Line Numbers
  1. String value = request.getParameter("paramName");
Familiar Sight
 
Join Date: May 2007
Location: Jaipur , India
Posts: 202
#5: Jul 6 '07

re: how to sort the table using combo box values


Hi,

In datadisplay.jsp page you are not getting value of k.So it is not working
Try following code

String k =request.getParameter("k");
Newbie
 
Join Date: Jul 2007
Posts: 10
#6: Jul 6 '07

re: how to sort the table using combo box values


Quote:

Originally Posted by r035198x

Put the value in a hidden input field on the form that's submitted. Then in the next page you get that value using

Expand|Select|Wrap|Line Numbers
  1. String value = request.getParameter("paramName");

Thank you very much. It is working fine.
Reply