Populate a combo box using data from a database
Hi Guys, In need of some help,
so i have three combo boxes,
the first is supplier,
the second range
and the final model
so what should happen is the user selects the first combo box which will then load the
next combo box values,
now i know how to populate the first one, and then how to send the data off to a servlet and preform a search on the database for the values I want,
but then how do I pass that back to the client using ajax, and then populate the combo box, I must'nt reload the page!
any help would be greatly appreciated!!!
View Answers
January 24, 2012 at 1:29 PM
1)country.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script language="javascript" type="text/javascript">
var xmlHttp
var xmlHttp
function showState(str){
if (typeof XMLHttpRequest != "undefined"){
xmlHttp= new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlHttp==null){
alert("Browser does not support XMLHTTP 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
}
}
</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`));
Ads
Related Tutorials/Questions & Answers:
Populate a combo box using data from a database
Populate a
combo box using data from a database Hi Guys, In need... to the client
using ajax, and then
populate the
combo box, I must'nt reload...
combo box which will then load the
next
combo box values,
now i know how
Acees data from database using combo box - JSP-Servlet
Acees
data from database using combo box please let me how i access the
data from database when i select
combo box combo2 having values Arts, Commerce, Science. this
combo box will appear when first
combo box class_name having
Advertisements
Retrieving Data from Database to fill Combo Box
Retrieving
Data from Database to fill
Combo Box Sir,
I have a JSP Page with a
combo box and a label. I have a
database that has two fields id and an image. Now I want to fill the
combo box with image and on selecting
Retrieving data from data base using jsp combo box
Retrieving
data from data base
using jsp
combo box Hi guys please...
combo box is there and another filed is version of the server(like 1.0,2.0) like... of the server it has to display the process name
from database into the process name
retrieving from oracle database using jsp combo box
retrieving
from oracle
database using jsp
combo box hi this is my... name of the server has to display in the process name field
from the oracle
database please help on this i need code
using servlets please help me .
<
Loading combo box from oracle
Loading
combo box from oracle how can i load values into a combobox
from oracle
database when a value is selected in another
combo box
Use of Select Box to show the data from database
Use of Select
Box to show the
data from database
 ... page to show
the
data fetched
from the
database. We are
using Servlet to get
data from the
database using MySQL and we are forwarding this servlet
data
populate dropdown box
populate dropdown box hi,
Is there any ways in which i can
populate my dropdown menu with values
from 1 to n (n = the value stored in
database)
using java script or jsp, either is fine?
Ex: If value corresponding to selected
retrieve data from database using jsf
retrieve
data from database using jsf Hello
I want an example of source code to retrieve
data from database
i have a
database (oracle) name as db1 it's contain table 'author' composed of three columns idauthor(primary key
how to get selected name from combo box
how to get selected name
from combo box i have fetched all records of worker i.e worker name. on a test.jsp
and dispayed it into
combo box ...my... of worker name within d
combo box and display that id into a text
box.
plz
how to get selected name from combo box
how to get selected name
from combo box i have fetched all records of worker i.e worker name. on a test.jsp
and dispayed it into
combo box ...my... of worker name within d
combo box and display that id into a text
box.
plz
how to get selected name from combo box
how to get selected name
from combo box i have fetched all records of worker i.e worker name. on a test.jsp
and dispayed it into
combo box ...my... of worker name within d
combo box and display that id into a text
box.
plz answer
how to get selected name from combo box
how to get selected name
from combo box i have fetched all records of worker i.e worker name. on a test.jsp
and dispayed it into
combo box ...my... of worker name within d
combo box and display that id into a text
box.
plz answer
how to get selected name from combo box
how to get selected name
from combo box i have fetched all records of worker i.e worker name. on a test.jsp
and dispayed it into
combo box ...my... of worker name within d
combo box and display that id into a text
box.
plz answer
combo box - JSP-Servlet
combo box I am
using a JSP form in which a combobox is given and I want to
populate data in it
from database and on the basis of yhe selected item I want to display related
data in a textbox Hi Friend,
Create