select option value

Ads
 

select option value

if i select a value of any drop down then that value should be used in a select query of the next dropdown in jsp????

View Answers

June 8, 2012 at 11:31 AM

Here is an example of dependent dropdown box in JSP. The values come from the database and stored into the first combobox. When the user selects the value from first combobox, the dependent values will get stored into second combo box through the MySQL database.

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

Ads









Related Tutorials/Questions & Answers:
select option value
select option value  if i select a value of any drop down...("Select * from country"); while(rs.next()){ %> <option value... id='state'> <select name='state' > <option value='-1
select option value
select option value  if i select a value of any drop down then that value should be used in a select query of the next dropdown in jsp on same page... buffer="<select name='state' ><option value='-1'>Select</option>
Advertisements
validate select option jquery
validate select option jquery  How to Validate select option in JQuery
Option onclick am not getting the value ..
Option onclick am not getting the value ..    function get_val( tot_val ) { document.getElementById('TextBox1').value = tot_val; } <..._val( tot_val ) { document.getElementById('TextBox1').value = tot_val
Jsp Option Value
;option value="">--select--</option> <option value...;/td> <select name="sel"><option value=""><---Select---><... Jsp Option Value   
how to select the row value that was retrived from the database ?
how to select the row value that was retrived from the database ?  I... in the page in which i am getting all the data from the database has an another select option as : Approve, Disapprove & Pending. Now if i select any option
how to select the row value that was retrived from the database ?
how to select the row value that was retrived from the database ?  I... in the page in which i am getting all the data from the database has an another select option as : Approve, Disapprove & Pending. Now if i select any option
how to select second combobox value .
how to select second combobox value .  I requirement is , i have two combo box, i am selected first combo box value then automatically second combo box value show ,But this both combo box value i retrieves in database. please
how to display textbox value based on selected option value?
how to display textbox value based on selected option value?  Hi,I have some problem. I use jsp and ajax. first select: 1 second select:2 based on first value I finished above code. ID: xx Name:xx xx is baesd on second
Add values of drop down menu( select option) to mysql table
Add values of drop down menu( select option) to mysql table   Here...['valveId'].'<td>'; echo '<select name="onoff">'; echo '<option value="ON">ON'; echo '</option>'; echo '<option
how to get the value of a label field of option tag in javascript?
how to get the value of a label field of option tag in javascript?  example <select> <option label="2" value="A">A</option> <option label="5" value="B">B</option> <
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...("$('#suggestions').hide();", 200); } $('#inputString').select(function
How to give value for select in HTML dynamically using javascript
How to give value for select in HTML dynamically using javascript  hi. Consider the situation i am having 2 select options called degree and department. if i select a degree the departments corresponding to that degree only
select value from autocomplete textbox using jquery in jsp from database.
select value from autocomplete textbox using jquery in jsp from database.  Hii Sir, Lots of thnx to ur reply .I went through both... of selecting value from autocomplete textbox using jquery in jsp from mysql database
select value from autocomplete textbox using jquery in jsp from database.
select value from autocomplete textbox using jquery in jsp from database.  Sir, Lots of thnx to ur reply .I went through both the tutorials but was unable to find out exact way to fullfill the solution of selecting value from
add text box and select list dynamically and get its value to store it into database using jsp request parameter
add text box and select list dynamically and get its value to store... dynamically added text field but want code to retrive value of dynamically added select box also into next jsp page plz reply me as early as posssible
i want to select a multiple value of check box by using onclick or onchange event without using from submission
i want to select a multiple value of check box by using onclick or onchange...)1000-1500 3)1500-2000 4)2000-2500 when i am selecting first value of checkbox the result is ok but after getting value of first box now i want a result of second box
Input type select
: <select name="country" size="2"> <option name="Australia" value...="Belgium" value="Belgium">Belgium</option> <option name="Germany" value="Germany">Germany</option> <option name="India" value="India" >
Select Box Validation in JavaScript
. Example : How to Create Select Box: <select> <option value="Delhi">... City <select name="city" id="city"> <option value="Select">Select<... and option tag inside the select tag is for available option in the list
how to calculate the price on the option box
how to calculate the price on the option box  How i calculate the value when i using a option box with 2 option..first option i used for product name... to calculate this value
JavaScript validate select tag
="n1"> <option value="Select">Select</option> <option value="10">10</option> <option value="20">20</option> <option value="30">30</option>
select query
select query  how to retrieve a single image from mysql table using $row[] value
nested select tag
("count"); String buffer="<select name='state'><option value='-1'>...' onchange="showState(this.value)"> <option value="none">Select<...; <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%><
. Display JavaScript dropdown selected value
="dropdown" onchange="selectDropdown()"> <option value="India" >India</option> <option value="US" >US</option> <option value="UK" >UK</option> <option value="Japan" >Japan</option> <option
ftech different value corresponding different value
ftech different value corresponding different value  in my dropdown two options are their and i want when i select any option the corresponding value... there are two optiond like delhi and mumbai so if a person select delhi then entry
JavaScript getElementById select
to perform any operation with the selected value as a option. We will be describing you... as document.getElementById().value. Similarly we have shown the selected option...; onChange="selectTypeOption();"> <option value="
Select from select list + display
"); while(rs.next()){ %> <option value="<...Select from select list + display  i have a select list containing... select EmpCode from the select list, the corresponding EmpName and DeptName should
For every option selected from a dropdown a new textbox opens
;select name="sel" onchange="generateTextbox();"> <option value="C/C++">C/C++</option> <option value="Java">Java</option> <option... have a dropdown with four options, each time I select an option a new text box
How to get more than one value from ajax
How to get more than one value from ajax  I have multiple select list box in php. i filled the value using ajax. how to get different value to fill the same list box Ex. option value as id option name as some string
Select DropDown Default
; <option value="">-=Show All=-</option>...; <option value="<%=rs.getString(1)%>"><...Select DropDown Default  Hi there, i have a program in JSP where i
Multiple select box
"); while(rs.next()){ %> <option value="<%=rs.getString("name")%>">...Multiple select box  Hi, I need help in code for multiple select box. The multiple select box should be populated with the db values.The selection

Ads