how to access values selected from connected comboboxes

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:
how to insert values from jsp into ms access
how to insert values from jsp into ms access   how to insert values using jsp into ms access database
how to access radio buttons selected in a servlet??...iam unable to retrieve values...it is returning NULL
how to access radio buttons selected in a servlet??...iam unable to retrieve values...it is returning NULL  <html> <head> <title>online exam</title> </head> <body> <div
Advertisements
how to access radio buttons selected in a servlet??...iam unable to retrieve values...it is returning NULL
how to access radio buttons selected in a servlet??...iam unable to retrieve values...it is returning NULL  <html> <head> <title>online exam</title> </head> <body> <div
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file  Hi am new to java. i need to create... in MS Access Database. the table structure is Sno JobName ProgramName Problem
How to store JComboBox selected Item into Ms Access Database - Java Beginners
How to store JComboBox selected Item into Ms Access Database  How to store JComboBox selected Item into Ms Access Database.  Hi Friend...:access","",""); Statement stmt=con.createStatement(); ResultSet rs
How to save JCombobox Selected Item in to Access Database - Java Beginners
How to save JCombobox Selected Item in to Access Database  How to save JCombobox Selected Item in to Access Database  Hi Friend, Try...:access","",""); Statement stmt=con.createStatement(); ResultSet rs
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 generate automatic bill based on selected values using jsp/javascript?
how to generate automatic bill based on selected values using jsp/javascript?  how to generate automatic bill based on selected values using jsp/javascript
How to save Selected text of RadioButton in to Access Databse - Java Beginners
How to save Selected text of RadioButton in to Access Databse  Sir I wnt To store Selected JRadioButton Text in to access Database plz Help Me Sir. and how to link website in to our Java Application with JRadioButton. 
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 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... 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... 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 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... 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 read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
How to know the selected row from table - JSP-Interview Questions
How to know the selected row from table  hi Every one....i am retriving data from database and i place that data into html table.in that table i place one table data as hyperlink. now the problem here is how can i know
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 jsp
How 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 backup a selected file from the dropDown Menu in JSP?
How to backup a selected file from the dropDown Menu in JSP?  I am trying to create a dropdown menu list for some type of files contained ina directory and upon selecting one, I'll have a click button backFile to copy the selcted
how to display the selected row from the data table in model panel ??
how to display the selected row from the data table in model panel ?? ...(rec3); } public ArrayList <TableData> selected() { Iterator<...(); return dataList; } please help me out !~!!!!! i get the model panel but the values
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 file
how 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 table
How 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-Servlet
how 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 jsp
How 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
how to get the values to dropdownlist from oracle database
how to get the values to dropdownlist from oracle database   </script> </head> <body> <select name... * from countryname"); while(rs.next()){ %> <option value="<
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
Dynamically display values in dropdown box and then show the selected values as selected by the user which is already stored in the DB
trying to display multiple selection selected from the list of all the values...Dynamically display values in dropdown box and then show the selected values..., This is for Updating. I had two SQL queries one for only selected values
Dynamically display values in dropdown box and then show the selected values as selected by the user which is already stored in the DB
selected values directly from the Database and not adding to it as an extra one...Dynamically display values in dropdown box and then show the selected values.... so if I have 5 values in the dropdown this selected one is added as 6th values
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
Inserting values into a database table of selected DropDown in jsp.
Inserting values into a database table of selected DropDown in jsp.  ... to insert all selected values in data base correspondent to each like if user selected india and then goa. I want to insert it to a table which has two fields
How to send the data selected from drop down menu from html page to sql 2005 database.
How to send the data selected from drop down menu from html page to sql 2005... by user from html drop down menu such as check-in date for hotel reservation system ,how can I save these data in database and how to retrieve later .Thanks
how to pass the mutiple values from <Ui:datagrid hyperlink - JSP-Servlet
how to pass the mutiple values from    I am getting the error when passing the values from this way emp2=${employee.lastName1} & name=${employee.firstName.How to pass the values from hyperlink in column
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database?  I want to store extracted xml values in a database... How can i store extacted xml values in a database... give me a example
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database?  I want to store extracted xml values in a database... How can i store extacted xml values in a database... give me a example
how to make a radiobutton selected when retrieving data from database using struts framework and spring jdbc
how to make a radiobutton selected when retrieving data from database using struts framework and spring jdbc  how to make a radio button selected when retrieving data from database using struts framework and spring jdbc
Java swing: get selected value from combobox
Java 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 Open Picture From M.S. Access Database using Java Servlet ?
How to Open Picture From M.S. Access Database using Java Servlet ?  Hi all my Friends I have below code which insert a picture into M.S. Access Database But i m still not able to open this picture through Java using M.S. Access
how to set values from parent page to iframe in javascript
how to set values from parent page to iframe in javascript  how to set values from parent page to iframe in javascript pls reply me as possible as I am in great trouble
How to edit values in textboxes from database using jsp
How 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
How to insert data from a combobox and textbox values into DB using JSP?
How to insert data from a combobox and textbox values into DB using JSP?  hi, How to insert a comb-box and a text box values in to DB using JSP? @DB:student; @table:stu_info; Combobox values:(class1,class2,class3); textbox1
how to pass the mutiple values from <Ui:datagrid hyperlink - JSP-Servlet
how to pass the mutiple values from      Hi Friend, Please post your full code. Thanks
how to read values from excel sheet and compare with database using jsp
how to read values from excel sheet and compare with database using jsp  hi sir i am arun how to read values from excel sheet and compare...,serialno) values of excelsheet we have to compare with database value if these 3
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir, I am working on a project in which i have to fetch the values... the database records from the table cells by entering new values there only

Ads