how to access values selected from connected comboboxes
how to access values selected from connected comboboxes for storing in database...i'm attatching a code i got from net for connected comboboxes...from this how could i access country and state....please help me....
1)state.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script language="javascript" type="text/javascript">
var xmlHttp
var xmlHttp
function showCity(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="city.jsp";
url +="?count=" +str;
xmlHttp.onreadystatechange = stateChange1;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChange1(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("city").innerHTML=xmlHttp.responseText
}
}
</script>
</head>
<body>
<select name='state' onchange="showCity(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 state");
while(rs.next()){
%>
<option value="<%=rs.getString("stateid")%>"><%=rs.getString("state")%></option>
<%
}
%>
</select>
<br>
<div id='city'>
<select name='city' >
<option value='-1'></option>
</select>
</div>
</body>
</html>
2)city.jsp:
<%@page import="java.sql.*"%>
<%
String state=request.getParameter("count");
String buffer="<select name='city'><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 city where stateid='"+state+"' ");
while(rs.next()){
buffer=buffer+"<option value='"+rs.getString(2)+"'>"+rs.getString(3)+"</option>";
}
buffer=buffer+"</select>";
response.getWriter().println(buffer);
}
catch(Exception e){
System.out.println(e);
}
%>
View Answers
March 15, 2012 at 11:06 AM
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:
Advertisements
How to access session values through Ajax?How to
access session
values through Ajax? Suppose in a servlet a variable userName is kept in session.
How can I
access this variable
from JSP through AJAX? Is it possible
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... answer me this
q. urgently.
With Regards,
Vishwnath
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... me this
q. urgently.
With Regards,
Vishwnath
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... me this
q. urgently.
With Regards,
Vishwnath
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... answer me this
q. urgently.
With Regards,
Vishwnath
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... me this
q. urgently.
With Regards,
Vishwnath
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
applet connected to table in MS Access database applet
connected to table in MS
Access database i have
connected my java code with the MS
access database and this is my code, can anyone tell me
how to show the table in an applet...pls
import java.sql.
How to pass multiple values from a servlet to JSP?How to pass multiple
values from a servlet to JSP? hi, I want to pass multiple
values form a servlet to JSP.
Hw do i do that?
I am able to send one value at a time without any prb, but I am unable to carry multiple (
from two
How to access the Title tag from xml to jspHow to
access the Title tag
from xml to jsp
How to
access the Title tag
from xml to jsp
Please visit the following link:
http://www.roseindia.net/jsp/parsing-xml.shtml
The above link will provide you an example
How to access (MySQL)database from J2ME?How to
access (MySQL)database
from J2ME? I am new to J2ME. I am using NetBeans.
Can anyone help me?
How to
access (MySQL)database
from J2ME?
( I search a lot I found that there is need to
access database through servlet
how to fetch values from .properties to a html filehow to fetch
values from .properties to a html file I have a .properties file with some key value pairs in it. I need to fetch the
values from this .properties file into a html file.Please let me know
how to do
How to carry multiple values from a Servlet to a JSP?How to carry multiple
values from a Servlet to a JSP? By using...).
----response.sendRedirect("index.jsp?uname="+username);----
But I want multiple
values needs to be carried
from my servlet to a JSP.
How do I do
How to Display values from databse into tableHow to Display
values from databse into table I want to display
values from database into table based on condition in query,
how to display... enter either bookname or authorname and click on search button the
values from How to access data yearly from DB in C# ?How to
access data yearly
from DB in C# ?
how to
access data yearly
from database in C#.
I have code but i m not able to retrieve data yearly
from... as Student_ID,payment
FROM fee" + ses + " where date1='" + date1
How to values from xml using java?How to
values from xml using java? Hi All,
I want to read value
from following < Line> xml using java.. In < Line>,data is in format of key and value pair.. i want to read only
values..could u plz help me
how to get a values from processRecord - JSP-Servlethow to get a
values from processRecord dear sir,
i have a problem in getting a
values from java to jsp.Here jsp is used to read a excel sheet that intern call a java program so i want a column
values i.e a column contains
How to access the following tag from xml to jspHow to
access the following tag
from xml to jsp
How can i get the title where cat="1"
and "My Title"
from XML File?
Please visit the following link:
http://www.roseindia.net/jsp/parsing-xml.shtml
The above link
How to create file from input values in Jframe ?How to create file
from input
values in Jframe ? hi i m doing my project using java desktop application in netbeans.i designed a form to get the user's academic details and on clicking the submit button,it displays all
ma access reading values values from "username and password"
from my ms
access............and detail...
values throught servlet and register.jsp
now i need code for reading
values from "username and password"
from my ms
access............and detail in register
ma access reading values has to login
. i already created ms
access page and inserted
values throught servlet and register.jsp
now i need code for reading
values from "username and password"
from my ms
access............and detail in register(table in my ms
Java swing: get selected value from comboboxJava swing: get
selected value
from combobox
In this tutorial, you will learn
how to get
selected value
from combobox.
The combobox provides the list... imposed an actionlistener on the combobox in order to
get the
selected value
from How to edit values in textboxes from database using jspHow to edit
values in textboxes
from database using jsp Hi RoseIndia,
I need help to solve my technical problem, i want to edit
values in textboxes
from database table using jsp, here is my code Please can anyone help me