Home Answers Viewqa HTML nested select tag

 
 


krishnachaitanya
nested select tag
4 Answer(s)      a year and 7 months ago
Posted in : HTML

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 Pages:
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
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
Mysql Nested Select
Mysql Nested Select       Mysql Nested Select is a SELECT query that is nested within a Select, Update... illustrate an example from 'MySQL Nested Select'. To understand and elaborate example
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
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
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  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
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 Example
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
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
Textarea Tag<html:textarea>:
; a textarea element. This tag is only valid when nested inside a form tag body... with the form tag we are nested within is utilized. property Name...Textarea Tag<html:textarea>: Information on Textarea Tag
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
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
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 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... to evaluate the contents contained in the nested body parts of this tag
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
Logic Present Tag (<logic:present >...</logic:present >)
; present tag -This tag evaluates its nested body contents...;/logic:not Present>)  notPresent tag -This tag evaluates its nested body... Logic Present Tag (<logic:present >...</logic:present >) 
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
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... contained in the nested body parts of this tag. This tag compares
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... then the nested body contents of this tag is evaluated Attributes
Hibernate Criteria Nested Properties
Hibernate Criteria Nested Properties Consider the example based on Hibernate Criteria Nested Properties DetachedCriteria detachedCriteria...); List list = criteria.list(); An example of Hibernate Criteria Nested
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... to evaluate the contents contained in the nested body parts of this tag. Tag
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 Tag... the contents contained in the nested body parts of this tag if the specified
Logic Empty Tag (<logic:empty>...</logic:empty>)
this tag to evaluate the contents contained in the nested body parts of this tag. Tag... contained in the nested body parts of this tag. Tag evaluation of the nested body... Logic Empty Tag (<logic:empty>...</logic:empty>) 
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
Hidden Tag <html:hidden>:
. If not specified, the bean associated with the form tag we are nested within is utilized...Hidden Tag <html:hidden>:       This tag facilitate  an HTML <input> element of type hidden
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 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 image tag but i am... image, but i am not able to use this in img tag of html
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
multiple select values
multiple select values   can you provide an example for multiple select values for html:select tag
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
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
JDBC Nested Resultset
JDBC Nested Resultset       The JDBC Nested Result Set  is the simplest join... you a code that helps in understanding JDBC Nested Result Set. The code include
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
Nested <c:forEach> in JSTL
of the tag for each element in the array or collection. The nested <c...Nested <c:forEach> in JSTL          The JSP Standard Tag Library
nested loops
nested loops  please tell input to get the output: 12345 23456 34567 45678
Optiontransferselect Tag (Form Tag) Example
. The Optiontransferselect tag is a UI tag that creates an option transfer select... select component. This tag contains various parameters: The label parameter... Optiontransferselect Tag (Form Tag) Example      
struts <html:select> - Struts
=db.getDbConnection(); PreparedStatement ps=con.prepareStatement("select Dealer_Code from op... in the Struts HTML FORM tag as follows: Thanks
Checkbox Tag <html:checkbox>:
valid when nested inside a form tag body. The property value associated...Checkbox Tag <html:checkbox>:       html: checkbox Tag -  is used to create a  Checkbox Input
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
JavaScript getElementById select
JavaScript getElementById select...; We can use document.getElementById() over the <select> tag... here to use getElementById with select by using a very simple example
Radio Tag <html:radio>:
; our current form. Note : This tag is only valid when nested inside a form..., the bean associated with the form tag we are nested within is utilized...Radio Tag <html:radio>:      
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.