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????

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









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
select option - JDBC
select option  how to dynamically generate the select option values from database
jsp drop down-- select Option
a country:</b>&nbsp;</td> <select name="sel"><option value... = stmt.executeQuery("Select * from country"); while(rs.next()){ %> <option value...; <select name='state' > <option value='-1'></option> <
Option Box Value - Java Beginners
Option Box Value  Hi Friends, I have one option box which is division, division have dynamically data,if user select any division then his option box is populated (work schedule,Peronal Area,personal sub area,business
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
select option with text facility - JSP-Servlet
select option with text facility  Hello. My doubt is regarding how to provide facility to add text in a drop down box ie(select tag in html )in a jsp... the select in html and if the user wants also to directly type the option instead
Jsp Option Value
<select name=""> <option value="">--select--</option> <option value="">--select--<...;<option value=""><---Select---></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
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... + one additional value in drop down or else normal user to view only drop down
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
jQuery change event with multiple select option
jQuery change event with multiple select option In this tutorial , we...; it also display the selected option. In the below example a multiple select list is given, when we select any option/options a change event is triggered
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 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
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
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
select query
select query  how to retrieve a single image from mysql table using $row[] value
html:select in struts - Struts
html select in struts  What is the HTML select in struts default value
Display JList value selected from the JOptionPane
Display JList value selected from the JOptionPane In this section, we are going to select the particular value from the JOptionPane combobox and displayed... will get displayed and allow the user to select a particular value. This value
select gender html
: <p> Select Gender:</p> <select name="gender"> <option value="none" selected>Gender</option> <option value="male">Male</option> <option value="female">Female</option> <
select gender html
: <p> Select Gender:</p> <select name="gender"> <option value="none" selected>Gender</option> <option value="male">Male</option> <option value="female">Female</option> <
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
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" >
Column select
Column select   How i fetch Experience wise resume?   ... or no. Then using the query select * from resumes where experience='yes', fetch all the data...("jdbc:mysql://localhost:3306/test", "root", "root"); String query = "select * from
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
ModuleNotFoundError: No module named 'option'
ModuleNotFoundError: No module named 'option'  Hi, My Python... 'option' How to remove the ModuleNotFoundError: No module named 'option'... to install padas library. You can install option python with following command
Select Box Validation in JavaScript
. Example : How to Create Select Box: <select> <option value="Delhi">...; Select your City <select name="city" id="city"> <option value="Select">Select</option> <option value="Delhi">Delhi</option>
how to use JOptionPane.YES_NO_OPTION
how to use JOptionPane.YES_NO_OPTION  As i said earliar i am making HRMS , the last phase is logout button. when logout button is clicked , an option dialog should appear which ask for yes or no . PROBLEM is i dont know how
select date in desired format
select date in desired format  how to select date from the database in DD/MM/YYYY format, because if i am trying select data from the database i am getting value in yyyy/mm/dd and time format like below "2012-05-07 00:00:00.0
dynamic select box
dynamic select box  thank u my dear friend.but i have a code like...; <input type="hidden" value="Leads" name="moduleName" /> <table>...;/label></td> <td><input type="text" name="lastname" value
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
SET and SELECT
SET and SELECT  hello, What is difference between SET & SELECT in sql????   hii,ADS_TO_REPLACE_1 SET : The set statement is used to the update query. SELECT : The select statement is used to display the all
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)%><
JavaScript getElementById select
to perform any operation with the selected value as a option. We will be describing you... option as document.getElementById().value. Similarly we have shown the selected...; onChange="selectTypeOption();"> <option value
select query
select query  how to write select query with where clause having multiple variables. example: i want to select the data from DB in which i want to check againest two variable in where cluase. String sql = "select * from
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
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>
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
The option tag
In this section, you will learn about the option tag of Spring form tag library
Min Value
Min Value   How to access min value of column from database table?   Hi Samar, You can access minimum value of column from database...= HibernateUtil.getSessionFactory().openSession(); String selectQuery ="select
where clause in select statement
where clause in select statement  In Mysql,I write the following query: select * from emp where designation = "MANAGER" But the actual value in the table is "manager" Will the above query retrieve any records? According to me

Ads