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!
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`));
Related Tutorials/Questions & Answers:
Populate a combo box using data from a databasePopulate 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-ServletAcees
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 BoxRetrieving
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 boxRetrieving
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 boxretrieving
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 oracleLoading
combo box from oracle how can i load values into a combobox
from oracle
database when a value is selected in another
combo box 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
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 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
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
populate dropdown boxpopulate 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
how to get selected name from combo boxhow 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 boxhow 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 boxhow 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
view data from database using drop down listview
data from database using drop down list hi i want to view the
data from database by selecting a value in a drop down list. for an example drop down list have picture element.when click it select pictures
from the
database combo box - JSP-Servletcombo 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
retrieve related data from database using jsp and mysqlretrieve related
data from database using jsp and mysql Hi sir,
please give some example of jsp code for retrieving mysql
database values in multiple dropdown list. if we change a value in a dropdown its related value must
combo boxcombo box Hi,
[_|] dropdown
box... a screen like this
using jsp-servlet(or DAO,DTO),in that drop down
box i should get usernames
from the
database & upon selection of particular user i should get