nested select tag

nested select tag

My requirement is as follows suppose combobox to select country whenever i select a country it will show corresponding states in another combo box.

View Answers

October 19, 2011 at 11:42 AM

JSP Code

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>

October 19, 2011 at 11:43 AM

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);
 }

 %>

3) For the above code, we have used 2 database tables:

1)country

CREATE TABLE `country` (                                 
           `countryid` bigint(255) NOT NULL auto_increment,       
           `countryname` varchar(255) default NULL,               
           PRIMARY KEY  (`countryid`)                             
     )

2)state

CREATE TABLE `state` (                                   
          `stateid` bigint(255) NOT NULL auto_increment,         
          `countryid` int(255) default NULL,                     
          `state` varchar(255) default NULL,                     
          PRIMARY KEY  (`stateid`)                               
        )

October 19, 2011 at 11:49 AM

<html>
ComboBox
<script language="javascript">
var arr = new Array();
arr[0] = new Array("-select-");
arr[1] = new Array("Maharashtra","Karnataka","Andhra Pradesh","Tamil Nadu");
arr[2] = new Array("Carinthia"," Styria");
arr[3] = new Array("Florida","New York","Maryland");
arr[4] = new Array("Queensland","Victoria","Tasmania","New South Wales");

function change(combo1){
var comboValue = combo1.value;
document.forms["form"].elements["combo2"].options.length=0;
for (var i=0;i<arr[comboValue].length;i++){
var option = document.createElement("option");
option.setAttribute('value',i+1);
option.innerHTML = arr[comboValue][i];
document.forms["form"].elements["combo2"].appendChild(option);
}
}
</script>
<form name="form" method="post"><select name="combo1" onchange="change(this);">
<option value="0">-Select-</option>
<option value="1">India</option>
<option value="2">Austria</option>
<option value="3">USA</option>
<option value="4">Australia</option>
</option>
</select><br />
<select name="combo2">
</select>
</form>
</html>

October 19, 2011 at 12:05 PM

Code in HTML

<html>
<h2>ComboBox</h2>
<script language="javascript">
var arr = new Array();
arr[0] = new Array("-select-");
arr[1] = new Array("Maharashtra","Karnataka","Kerela","Rajashthan");
arr[2] = new Array("Texas","New York","Florida","California");

function change(combo1)
{
var comboValue = combo1.value;
document.forms["form"].elements["combo2"].options.length=0;
for (var i=0;i<arr[comboValue].length;i++)
{
var option = document.createElement("option");
option.setAttribute('value',i+1);
option.innerHTML = arr[comboValue][i];
document.forms["form"].elements["combo2"].appendChild(option);
}
}
</script>
<form name="form" method="post">
<select name="combo1" onchange="change(this);">
<option value="0">-Select-</option>
<option value="1">India</option>
<option value="2">USA</option>
</option>

</select><br />
<select name="combo2">
</select>
</form>
</html>









Related Tutorials/Questions & Answers:
nested select tag
nested select tag  My requirement is as follows suppose combobox to select country whenever i select a country it will show corresponding states...; </head> <body> <select name='country
Mysql Nested Select
Mysql Nested Select       Mysql Nested Select is a SELECT query that is nested within a Select, Update... The Tutorial illustrate an example from 'MySQL Nested Select'. To understand
Advertisements
MySQL Nested Select Example
MySQL Nested Select Example       MySQL Nested Select is select query, that is nested inside... Clause. The Nested Select returns you the set of records from table based
struts2 select tag multiple
struts2 select tag multiple  struts2 select tag multiple
The select tag
In this section, you will learn about the select tag of Spring form tag library
Validate <select> tag Items
Validate select tag Items  Hi, How to validate addition of two numbers of two different "select" tag items in JavaScript..? Thanks in advance
Validate <select> tag Items
Validate select tag Items  Hi, How to validate addition of two numbers of two different "select" tag items in JavaScript..? Thanks in advance
select tag multiple values
select tag multiple values   I want to insert multiple values in database which i have selected from select tag
About Select tag
About Select tag  hello Sir I am using select tag in jsp page and i want to change the background color of label which I have given to the select... in select tag please give me the solution my code as follows : <s
about select tag
about select tag  Hi, i have a doubt regarding the usage of select tag,that is,suppose i am having two dropdown boxes,based on the selection of one dropdown item another dropdown item will be displayed without interacting
nested selected tag ihave display selected item
nested selected tag ihave display selected item   i have two combo boxes combo1 combo1 in first combo box i taken as follows select name="combo1"> option value="0">-select- option value="1">INDIA option value
Struts nested tag Example
Struts nested tag Example       The tag library ?nested? is included in Struts... tag library and how you can use it. We can manage nested beans easily
Select Tag (Form Tag) Example
Select Tag (Form Tag) Example       In this section, we are going to describe the select tag. The select tag is a UI tag that is used to render an HTML input tag of type select. Add
validate select tag items in javascript
validate select tag items in javascript  Hi, How to validate addition of two numbers from two different <select> tag items in JavaScript..? Thanks in advance
JavaScript validate select tag
JavaScript validate select tag In this tutorial, you will learn how to validate html select tag. Here, we have used two select tags to select numbers in order to calculate addition of two numbers. If the user does not select any
select tag in Struts2 to handle Enums - Struts
select tag in Struts2 to handle Enums  I have an java enum in my object. I am trying to set its values from struts2 select tag. I tried with "#list... to handle enums in struts2 select tag ?   Hi friend, Code to solve
How to add another option to the select tag using struts2 tag - Struts
How to add another option to the select tag using struts2 tag  Hi, How to add another option to select tag using tag. My scenario is : If the logged in user is admin then drop down should contain the normal list
Select tag to fetch data from oracle database
Select tag to fetch data from oracle database  I created a select box having more than one menus in the select box such as regnno, address and name of a student and when regnno is selected from the drop down list by a user
JSP:select image for db and display in image tag
JSP:select image for db and display in image tag  Hi, i am new to this forum. My query is that, i am trying to display image in tag but i am..., but i am not able to use thin in tag of html
JSP:select image for db and display in image tag
JSP:select image for db and display in image tag  Hi, i am new to this forum. My query is that, i am trying to display image in tag but i am..., but i am not able to use this in tag of html
JSP:select image for db and display in image tag
JSP:select image for db and display in image tag  Hi, i am new to this forum. My query is that, i am trying to display image in image tag but i am... image, but i am not able to use this in img tag of html
MySQL Nested Example
; MySQL Nested Select is select query, that is nested inside the select.... The Nested Select returns you the set of records from table based on the condition of nested select query specified in Where Clause. Understand with ExampleADS
we want to send requiest to server by html select tag - JSP-Servlet
we want to send requiest to server by html select tag  how we can send requiest to server using html select tag. and pass the value also  Hi Request Information In JSP Request Method
Mysql Nested Case
Mysql Nested Case       Mysql Nested Case is a Select Query that is nested inside select, update...' which are nested in the select query of table 'employee
dynamic generation of html:select tag from textbox value
dynamic generation of html:select tag from textbox value  Hi, I am a newbie to java and struts. In my application, I have a requirement like when i give some input in a textbox and click on a add link. It should be added
dynamic generation of html:select tag from textbox value
dynamic generation of html:select tag from textbox value  Hi, I am a newbie to java and struts. In my application, I have a requirement like when i give some input in a textbox and click on a add link. It should be added
Logic Equal Tag (<logic:equal>...</logic:equal>)
then this tag is used to evaluate the contents contained in the nested body parts.... The nested body content of this tag is evaluated if the variable and value... in the nested body parts of this tag. Compares the variable passed
Textarea Tag<html:textarea>:
; a textarea element. This tag is only valid when nested inside a form tag body... associated with the form tag we are nested within is utilized. property...Textarea Tag<html:textarea>: Information on Textarea Tag
GROUP BY IN NESTED QUERY
GROUP BY IN NESTED QUERY  Hello Every One, Query to use GROUP BY CLAUSE IN NESTED QUERY SELECT STATEMENT in mysql database.If u know please help me.thanks
Logic Present Tag (<logic:present >...</logic:present >)
; present tag -This tag evaluates its nested body contents...:not Present>)  notPresent tag -This tag evaluates its nested body content if the specified value is not present in the request. Tag evaluation of the nested
Optiontransferselect Tag (Form Tag) Example
. The Optiontransferselect tag is a UI tag that creates an option transfer select... an option transfer select component. This tag contains various parameters... Optiontransferselect Tag (Form Tag) Example      
Nested class
Nested class  What is nested class?  when a class is defined within another class then such a class is called a nested class. Nested... nested classes are: It is a way of logically grouping classes that are only used
Logic LessEqual Tag (<logic:lessEqual>...</logic:lessEqual>)
contained in the nested body parts of this tag. This tag compares the variable... to the specified value then the nested body contents of this tag is evaluated... then we use this tag to evaluate the contents contained in the nested body parts
Logic greaterEqual Tag (<logic: greaterEqual>... </logic:greaterThan>)
in the nested body parts of this tag. This tag compares the variable against... to the specified value then the nested body contents of this tag is evaluated...;/ logic:greaterThan>)  greaterThan Tag : Tag evaluation of the nested body
Logic Present Tag (<logic:equal>...</logic:equal>)
this tag to evaluate the contents contained in the nested body parts of this tag. Tag evaluation of the nested body content occurs only if the specified value... in the nested body parts of this tag. Tag evaluation of the nested body content occurs only
Struts2.2.1 updownselect Tag Example
Struts2.2.1 updownselect Tag Example The updownselect tag is a UI tag that creates a HTML select component with buttons to move up and down the elements in the select component. When the containing form is submitted, its elements
nested loops
nested loops  please tell input to get the output: 12345 23456 34567 45678
how to use group by in nested query
how to use group by in nested query  Hai, How to use group by clause in nested query select statement in subquery i am having where clause.please help me.thanks in advance
Logic Match Tag (<logic:match >...</logic:match >)
in the nested body parts of this tag if the specified value is an appropriate substring...; then the nested body content of this tag is evaluated. Attributes of match TagADS... contained in the nested body parts of this tag if the specified value
JSF selectItem Tag
about the selectItem tag. It can be used with any select tag of JSF html tag...JSF selectItem Tag          This tag is used to add a child component
Logic Empty Tag (<logic:empty>...</logic:empty>)
this tag to evaluate the contents contained in the nested body parts of this tag. Tag... to evaluate the contents contained in the nested body parts of this tag. Tag... Logic Empty Tag (<logic:empty>...</logic:empty>) 
HTML5 select tag, Implementation of &lt;select&gt;tag in HTML5.
HTML5 select tag, Implementation of <select> tag in HTML5. In this section, you will learn about <select> tag of HTML5. The select tag is use... of select tag.</h1>   <form id="Selectform1">   
Nested Clases
Nested Clases  public class Nestclasses { a obj=new a(); public..."; System.out.println("hellwo in nested class"+i+ch); } } } class Outer..."); } } } /*Q:-Here I innner Class Constructor but its not call why i
program to create nested tables
program to create nested tables  program to create nested tables
JSF selectItems Tag
JSF selectItems Tag          This tag is used to add a set of items to the nearest enclosing parent (select one or select many) component. This tag can be used
Nested try
Nested try  Hi, In eclipse without providing input i am getting different outputs ie my exception gets executed first then finally block and vice versa.pl explain me class Demo { static void nestedTry(String args[]) { try
nested for-loop
nested for-loop  hi.write a program that will prompt the user for a positive integer(num) and then display two triangles with num number of lines to represent the following patterns of asterisks. the input num must be validated
Nested try
Nested try  Each time when i execute this code in eclipse i get different answers pl help me with this code class Demo { static void nestedTry..., visit the following link: Nested Try Example
Select Tag&lt;html:select&gt;:
html:select Tag : Creates a HTML <select> element, associated with a bean property specified by our attributes. Select Tag<html... by our attributes.  Note: This tag is only valid when nested inside
Nested classes: Examples and tutorials
Nested classes: Examples and tutorials       Nested classes Here is another advantage of the Java... within another class, such class is called a nested class. Inner classes can

Ads