How to get sub category based on category from database using jsp and javascript?
November 5, 2012 at 3:08 PM
Here is a jsp code that generates dependent dropdown box.
1)country.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script language="javascript" type="text/javascript">
var xmlHttp
var xmlHttp
function showState(str){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="state.jsp";
url +="?count=" +str;
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChange(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("state").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
</head>
<body>
<select name='country' onchange="showState(this.value)">
<option value="none">Select</option>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * from country");
while(rs.next()){
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>
<%
}
%>
</select>
<br>
<div id='state'>
<select name='state' >
<option value='-1'></option>
</select>
</div>
</body>
</html>
2)state.jsp:
<%@page import="java.sql.*"%>
<%
String country=request.getParameter("count");
String buffer="<select name='state' ><option value='-1'>Select</option>";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * from state where countryid='"+country+"' ");
while(rs.next()){
buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(3)+"</option>";
}
buffer=buffer+"</select>";
response.getWriter().println(buffer);
}
catch(Exception e){
System.out.println(e);
}
%>
For the above code, we have created two database tables:
CREATE TABLE `country` (
`countryid` bigint(255) NOT NULL auto_increment,
`countryname` varchar(255) default NULL,
PRIMARY KEY (`countryid`));
CREATE TABLE `state` (
`stateid` bigint(255) NOT NULL auto_increment,
`countryid` int(255) default NULL,
`state` varchar(255) default NULL,
PRIMARY KEY (`stateid`));
Related Tutorials/Questions & Answers:
Advertisements
How to get data from Oracle database using JSPHow to
get data
from Oracle
database using JSP hello i have a simple problem in
jsp in the sense to
get data
from the
database like oracle . I have... the answer to retrieve data
from the
database by
using servlet or
jsp program
how to get data from database into dropdownlist in jsphow to
get data
from database into dropdownlist in jsp Can anybody... tutorial go through the link
JSP Get Data Into Dropdown list
From Database
... the
database and set it into dropdown list in
jsp
please help
<br/>
how to display values from database into table using jsphow to display values
from database into table
using jsp I want to display values
from database into table
based on condition in query,
how... the values
from database based on the bookname or authorname entered must be display
JSP Get Data From DatabaseJSP Get Data
From Database
In this section we will discuss about
how to
get data
from database using
JSP.
To
get data
from database to a
JSP page we.... In my example I am
using
the MySQL
database system and the provided driver
Need query for rollup based on month and category Need query for rollup
based on month and
category Hi,
Here is my question..
I have a table as below.
trans_date,cateory,amount
1-1-11,1,10
2-1-11,2,20
3-2-11,3,15
23-2-11,4,12
13-5-11,4,19
Need a query to
get result like
How to access the database from JSP?How to access the
database from JSP? Hi,
What is the process of accessing the
database from JSP page?
Thanks
Hi,
In the
JSP program...
database from JSP which explains you
how to access the
database by embedding
How to retrieve image from mysql database in JSP?How to retrieve image
from mysql
database in
JSP? Hi,
I need
JSP same codes for learning to
get image which is stored in MySQL
Database.
How... SQL query to find the data
from database and
get the image
from resultset
using retrive data from database using jsp in struts? retrive data
from database using jsp in struts? *search.jsp*
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<... searchProduct(SearchDTO sdto) {
String query="select *
from product
get info from mysql using jsp and servletget info
from mysql
using jsp and servlet HELLO! I wanna create a
jsp page which able to let me
get its name, phone and other info by asking the user to key in their email address
from mysql
database by
using servlet and
jsp too
fetch record from oracle database using jsp-servlet?fetch record
from oracle
database using jsp-servlet?
how can i fetch data
from oracle
database by
using jsp-servlet. i'm
using eclipse, tomcat server and oracle
database and creating
jsp pages and also
using servlet