Breaking News

Saturday 1 March 2014

How to use Ajax to load dynamic values in select dropdowns


There are every day scenarios where one needs to have a working snippet of commonly used functions.
Keep checking the posts here with tags Code Snippet. Just use them and make your development faster.

In Some cases loading of vales for a select drop down may be dependent on values selected from other drop down. this kind of feature is normally seen when we select country, state and city.

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
      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>
 <%@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=buffer+"</select>";
 response

 response.getWriter().println(buffer);
 

 }
 catch(Exception e){
     System.out.println(e);
 }
 %>
CREATE TABLE `country` (                                
           

           `countryid` bigint(255) NOT NULL auto_increment,      
           

           `countryname` varchar(255) default NULL,              
           PRIMARY KEY  

           PRIMARY KEY  (`countryid`));



CREATE TABLE



CREATE TABLE `state` (                                  
         

          `stateid` bigint(255) NOT NULL auto_increment,        
         

          `countryid` int(255) default NULL,                    
         

          `state` varchar(255) default NULL,                    
          PRIMARY KEY  

          PRIMARY KEY  (`stateid`));

In this example's are hard coded.., soon i will attach a sample application where you can download a working copy of this example.
Thanks

2)state.jsp:
For the above code, we have created two database tables:

No comments:

Post a Comment

Designed By