how to load value in dropdown list after selecting value from first dropdown list using javascript

how to load value in dropdown list after selecting value from first dropdown list using javascript

how to load value in dropdown list after selecting value from first dropdown list using javascript

View Answers

March 16, 2012 at 10:54 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 load value in dropdown list after selecting value from first dropdown list using javascript
how to load value in dropdown list after selecting value from first dropdown list using javascript  how to load value in dropdown list after selecting value from first dropdown list using javascript
how to set value of dropdown list
how to set value of dropdown list  Hello Sir, I'd solved the earlier problems somehow but now this time another problem arised when I want to pass the value from a link to a dropdown list. I want to set the dropdown list selected
Advertisements
how to set value of dropdown list
how to set value of dropdown list  Hello Sir, I'd solved the earlier problems somehow but now this time another problem arised when I want to pass the value from a link to a dropdown list. I want to set the dropdown list selected
help to select a value from the dropdown list
help to select a value from the dropdown list  I have a html file...()); } %> Please can someone help me on how to select a value from the drop down list created in a div and put it in the textbox kim
how to use dropdown list in JSP and display value in same page
how to use dropdown list in JSP and display value in same page  I have a Dropdown list with some values say "A", "B" and "C" When the user selects... in the same page Value selected = C How can we do this in a JSP page
Retriving value from dropdown list nad disply it in other page
Retriving value from dropdown list nad disply it in other page  Hello, I have a dropdownbox on my webpage and a map which contains a marker... of the selected value from the dropdown list and dispaly it to on the next page. i
. Display JavaScript dropdown selected value
. Display JavaScript dropdown selected value  How to get the selected value from dropdown box? I am using javascript.   <html> <...;/html> Description: Create dropdown list by using select and option tag. Now when
how to perform the client side validations to the dropdown list using javascript?
how to perform the client side validations to the dropdown list using javascript?  Hi Good Morning! This is Prasad, I want to perform the client side validation to the dropdown list using javascript. So give me sample.I am
How to automatically change the value of the textbox based on the dropdown list?
How to automatically change the value of the textbox based on the dropdown list?  I want to know how to automatically change the value of price...; <script type="text/javascript" src="js/jquery.js"></script> <
How to display content in same page while selecting fron dropdown list
How to display content in same page while selecting fron dropdown list ... dropdownlist in jsp page.If i select any value in that list(for eg-rollno... at the JSP page will be interpreted by the browser, first. You can use Javascript
3 dropdown list from the database using JSP
3 dropdown list from the database using JSP  Hi, I'm new to JSP I want to create 3 dropdown list each depend on the other and get the options from the database using JSP
How to display content in same page while selecting fron dropdown list
How to display content in same page while selecting fron dropdown list  Thanks for your suggesstion.but Please provide me coding by using only one dropdownlist in javascript or jquery.Thanks in advance
How to access Contacts from gmail using LDAP in JSP dropdown list
How to access Contacts from gmail using LDAP in JSP dropdown list  HI, I am creating a JSP page in which i have to make a dropdown box, and access gmail contacts in that drop downlist using LDAP. Can any one plz help me out
getting values from dropdown list
getting values from dropdown list  I am having a dropdown list which has hardcoded values ( we need it hardcoded only and will not be populated from the database) My question is when i select a particular value it should be pass
DropDown list
DropDown list  how to get mysql database values into dropdown usign... values into dropdown list. 1)country.jsp: <%@page import="java.sql.*"%> <html> <head> <script language="javascript" type
JSP Get Data Into Dropdown list From Database
JSP Get Data Into Dropdown list From Database In this section we will discuss about how to fetch data dynamically into the dropdown list in JSP... fetching of data into the dropdown list in JSP using Eclipse IDE and the Tomcat 7
dropdown list in jsp
dropdown list in jsp  hai, i have static dropdown list.. i want to get the selected value in string variable without jsp regards asha
populating one dropdown box depending on the value of other using jsp
populating one dropdown box depending on the value of other using jsp ... on the value of other using the below table country name state and city name should load in dropdown box one after the other CREATE TABLE CITY_MASTER( CITYID BIGINT
how to add data dynamically from database into a dropdown list in a jsp
how to add data dynamically from database into a dropdown list in a jsp ... list.In that first list is for department and the 2nd list for the names of the persons who work in that department.The values into the drop down list should
creating list in dropdown using struts - Struts
creating list in dropdown using struts   creating list in dropdown using struts : In action class list.add(new LabelValueBean("ID","Name")); In Jsp * Select Item Select In Form : getter
Retrieval of Dropdown list
, the corresponding value of A1, list of name corresponding to A1 wil be displayed in another...Retrieval of Dropdown list  Hi frnds... Am having problem with the below code... I have retrieved data from the mysql database into dropdownlist
struts dropdown list
struts dropdown list   In strtus how to set the dropdown list values from database ? I have a ArrayList object and set this object in dropdown jsp page using struts ? please send me jsp code... sample code: **Action
to access value of drop down value in beans when dropdown is populated using ajax
to access value of drop down value in beans when dropdown is populated using ajax  i want to access value of dropdown box in beans .. and this drop down was filled using ajax. plz help
dropdown list in jsf
dropdown list in jsf  I want to add a list box to display the country name from the lists on all countries.When I select for e.g India then in the second list box it will display the states related to India only and the flow
How to add a DropDown List in Flex DataGrid
How to add a DropDown List in Flex DataGrid  hi I am trying to add a DropDownList in a DataGrid table. After the user selects one of the items from... select from one of the choices in the list. That's my reason for trying to add
dropdown list and text fields in php
dropdown list and text fields in php  How could I use php to populate text fields by selecting a name of a business from dropdown list? Those text... number, description, and opening times from a mysql database. If you can, post
How to add a DropDown List in Flex DataGrid
How to add a DropDown List in Flex DataGrid  hi I am trying to add a DropDownList in a DataGrid table. After the user selects one of the items from... select from one of the choices in the list. That's my reason for trying to add
how can i send a mail to a particular user from a many user dropdown list in jsp
how can i send a mail to a particular user from a many user dropdown list in jsp  how can i a sent a user question to a particular person from a drop down list in jsp
jsp- database dependent dropdown list
jsp- database dependent dropdown list   i want 2 dropdown list 1- CLASS 2-SECTION both are should come from database. and if i select a class suppose class a the corresponding section dropdown list should retrieve the section
How to add dropdown list in a row of a sort table applet?
How to add dropdown list in a row of a sort table applet?  How to add dropdown list in a row of a sort table applet
Hi, I'm new to JSP I want to create 3 dropdown list each depend on the other and get the options from the database using JSP
Hi, I'm new to JSP I want to create 3 dropdown list each depend on the other and get the options from the database using JSP  as i said i want to create 3 drop dropdown list each depend on the other and get the options from
Dropdown Checkbox list
Dropdown Checkbox list This tutorial explains how to create jquery Dropdown... jquery Dropdown Checkbox list. The application directory structure should look like... || []).push({}); Step 1 : First we create Dynamic web project "dropdown
Selecting value from autocomplete textbox using jquery in jsp
Selecting value from autocomplete textbox using jquery in jsp   hello Sir, I completed ur tutorial on autocompletion textbox from database using... to select that autosuggested value from database to textbox.plz give me
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application  Hello Sir, first of all... a dropdown menu to choose type of report and then click on submit button.On clicking
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application  Hello Sir, first of all... a dropdown menu to choose type of report and then click on submit button.On clicking
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application  Hello Sir, first of all... a dropdown menu to choose type of report and then click on submit button.On clicking
getting and storing dropdown list in database in jsp
getting and storing dropdown list in database in jsp  i have a drop down list to select book from database. i'm able to retrieve dropdown list from database. but unable to store the selected value in database table. please help
passing value from javascript popup
passing value from javascript popup  How to take user input and pass that input value using popup in Javascript
populating textbox value from the database using onchange function,
selecting from dropdownlist. i have a form in jsp n I created the dropdown list dynamically from the database(mysql). after selecting the value from the dropd...populating textbox value from the database using onchange function,  
ModuleNotFoundError: No module named 'django-admin-list-filter-dropdown'
: ModuleNotFoundError: No module named 'django-admin-list-filter-dropdown' How to remove... After the installation of django-admin-list-filter-dropdown python library...ModuleNotFoundError: No module named 'django-admin-list-filter-dropdown
How to Fetch Data using dropdown option
How to Fetch Data using dropdown option   sir maine jsp pr ek login page banaya jismain branch ka option hai. main agar first dropdown main first year branch select karta hoon to first year ki sari branches ki list second drop
dropdown
dropdown  how to hide textbox field when i deselect from select dropdown
jsp login code when username , password and dropdown box value is correct...
jsp login code when username , password and dropdown box value is correct... in dropdown box.... so when i login i all the three username,password and dropdown box value(i.e)company name should match.... pls provide code.... thanx   1
jsp login code when username , password and dropdown box value is correct...
jsp login code when username , password and dropdown box value is correct... in dropdown box.... so when i login i all the three username,password and dropdown box value(i.e)company name should match.... pls provide code.... thanx   
how to maKE Jcombox editable after saveing value init.
how to maKE Jcombox editable after saveing value init.  i have one JCombobox with 2 values"y", "N".when i selecting some value from dropdown... the value and when i clicked on save unable to save it. how to maKE Jcombox editable
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 read value from xml using java?
How to read value from xml using java?  Hi All, I want to read value from following 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 in this?Thanks
I tried to create a dropdown list using struts2.it is not working.can you find the errors in this code?
I tried to create a dropdown list using struts2.it is not working.can you find the errors in this code?  /jsp code for creating dropdownlist using...="design" /> /*dropdown list included*/ <p:submit name="submit"/>
Populate dropdown menu from database using jsp and servlet
Populate dropdown menu from database using jsp and servlet  please i need code to populate dropdown menu from mysql database using jsp and servlet. thanks
dropdown
dropdown  how to select an item from the dropdownlist when we load...("sel"); var str = v.options[v.selectedIndex].value; alert(str); } <...;option value="C">C</option> <option value="JAVA">JAVA</option>

Ads