Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners how to populate second dropdownlist on selecting items from first one using jsp .data should be retrived from database.I prefer javascrpit.can anyone help me
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Darsana K
how to populate second dropdownlist on selecting items from first one using jsp .data should be retrived from database.I prefer javascrpit.can anyone help me
2 Answer(s)      2 years and 3 months ago
Posted in : Java Beginners


how to populate second dropdownlist on selecting items from first one using jsp .data should be retrived from database.I prefer javascrpit.can anyone help me

hello everyone ' im beginner in java i want to know populating dropdownlist from db. for eg ; i hv 2 dropdowns project and mission.On loading jsp project should be populated from database.On selecting project type from dropdwon1 corresponding mission type should be loaded on dropdown2 ie,mission i hv a table proj-mission for that.


Advertisement
View Answers

December 7, 2012 at 12:26 PM


Here is an example of dependent dropdown list.

1)country.jsp:

<%@page import="java.sql.*"%>
 <html>
      <head>  
      <script language="javascript" type="text/javascript">  
      var xmlHttp  
      var xmlHttp
      function showState(str){
      xmlHttp=GetXmlHttpObject();
      if(xmlHttp==null){
       alert ("Browser does not support HTTP 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   
      }   
      }
     function GetXmlHttpObject(){
         var xmlHttp=null;
         try{
          xmlHttp=new XMLHttpRequest();
         }
         catch(e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
      </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>


December 7, 2012 at 12:27 PM


continue..

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 store the data in a array retrived from the database - JSP-Servlet
in database,and i need to disply all the employees record in a single jsp. pls can any one tell me how to do it this using jsp. thankyou,  Hi nagaraj...how to store the data in a array retrived from the database  hi
display data from database on selecting option of dropdownlist
display data from database on selecting option of dropdownlist   Sir I want to display data from database by just selecting option of dropdown list without using any button in a table format and one more addtional question
Advertisements
display data from database on selecting option of dropdownlist
display data from database on selecting option of dropdownlist   Sir I want to display data from database by just selecting option of dropdown list without using any button in a table format and one more addtional question
display data from database on selecting option of dropdownlist
display data from database on selecting option of dropdownlist   Sir I want to display data from database by just selecting option of dropdown list without using any button in a table format and one more addtional question
display data from database on selecting option of dropdownlist
display data from database on selecting option of dropdownlist   Sir I want to display data from database by just selecting option of dropdown list without using any button in a table format and one more addtional question
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp  Can anybody tell me what is the problem in this code as i am not able to fetch the data from... tutorial go through the link JSP Get Data Into Dropdown list From Database   
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp  Customer Name...=con.createStatement(); String sql="select * from staff"; ResultSet rs... tell me what is the problem in this code
how to output text from comboBox from first frame to textField on the second frame? please help me.. thank you in advance..
how to output text from comboBox from first frame to textField on the second frame? please help me.. thank you in advance..  how to output text from comboBox from first frame to textField on the second frame? please help me
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp  //Customer Name: <select name="name"> <% try{ Class.forName...="select * from staff"; ResultSet rs=st.executeQuery(sql); while(rs.next
Populate a combo box using data from a database
to populate the first one, and then how to send the data off to a servlet and preform...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
how to get data using dropdownlist, textbox and search button in jsp
how to get data using dropdownlist, textbox and search button in jsp  Hi, I want to display data using jsp,javascript and mysql. My Q. is If i select an entry suppose 'title' from dropdownlist and give a title name 'partial dif
Second tab should only show selected products from first tab
Second tab should only show selected products from first tab  In the javascript, From the comment â?? // live update of how many circlesâ?? on. The first tabâ??s form has been built manually. It is in the â?
displaying List of records from database in a jsp using ajax, onclick it should display the results ?? its urgent can u help me
displaying List of records from database in a jsp using ajax, onclick it should display the results ?? its urgent can u help me   displaying List of records from database in a jsp using ajax, onclick it should display the results
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
By dropdownlist retrive data from database and show in textbox in jsp.
By dropdownlist retrive data from database and show in textbox in jsp.  Hello Sir, I am doing project in jsp.I have to retrive data from database, when I select value from dropdownlist, and that data I want to show in textbox
problem in selecting second arraylist from mysql database
, but i want second list to be dynamic from database. Please, help me out. Thank...problem in selecting second arraylist from mysql database  Hi...', it should give list of that selected one in second list, BUT THAT list should be stored
selecting data from database
selecting data from database  how to select data from database using Dao in struts   Hi, You have to load the data from database using hibernate and send the data in a Java file object to jsp page. Please read at http
dynamic retrival of data from mysql database to dropdownlist in jsp
dynamic retrival of data from mysql database to dropdownlist in jsp  Hello, Am having problem in my project... i want to retrive the data from mysql database to the dropdownlist in jsp. Example: 1st i want to display the list
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
how to pass arraylist data from one jsp to another jsp - JSP-Servlet
how to pass arraylist data from one jsp to another jsp  hi to all iam... to receive all employee id's that are retrived from the database in the first jsp. can any one help me. ThankU  Hi nagaraj can u please send your
Values from servlet into dropdownlist in jsp page using ajax
Values from servlet into dropdownlist in jsp page using ajax  1) jsp...); } } } Im not able to get values retrived from servlet into dropdownlist of jsp page...kindly check the code attached and tell me
How to display data in jsp from dao using java beans?
How to display data in jsp from dao using java beans?  Hi I need to display data in jsp pulling from dao using java beans, Please can anyone give me the sample application with above topics. Any help would be highly appreciated
How to add value from sql server in dropdownlist
How to add value from sql server in dropdownlist  I have created a hospital management project using jsp servlet, where one form name is patient... id from other table like out patient, and this id will be stored dynamically
How to get data from DB in to Text box by using Jsp & Ajax
How to get data from DB in to Text box by using Jsp & Ajax   I want to get the data from database in to text box in a jsp page by using Ajax. If I... with a and from that i need to select the required value and i should store
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 export data from jsp to excel sheet by using java
How to export data from jsp to excel sheet by using java   How to export data from jsp to excel sheet by using java
How to get the data from the database using Servlet or JSP program
How to get the data from the database using Servlet or JSP program  ... database using JSP Get data from database using JSP... from the database like oracle . I have created one jsp program like this <
How to get data from Oracle database using JSP
How to get data from Oracle database using JSP  hello i have a simple... data from the database like oracle), I have created one jsp program like... the answer to retrieve data from the database by using servlet or jsp program
How to forward the control from one jsp to another?
How to forward the control from one jsp to another?  Hi... other.i.e. First JSP is communicating with the second JSP, Second JSP is not communicate with the Third JSP. I want forward the control from first jsp to second
hello. can anyone help me with my java project, using jcreator?
hello. can anyone help me with my java project, using jcreator?  this is how it goes.. we are to develop a GUI of a certain form, in my case this one and then i should be able to create, view, search, and save using swamp.. i
how can i display the dates of one week by selecting the date randomly from the calendar?
how can i display the dates of one week by selecting the date randomly from the calendar?  I am facing a problem in my application,Here i should... the date selected from the calendar. how can we display the dates by picking
 

 

 

DMCA.com