Home Answers Viewqa JSP-Interview-Questions jsp drop down-- select Option

 
 


sandy
jsp drop down-- select Option
2 Answer(s)      2 years and 7 months ago
Posted in : JSP-Interview Questions

how to get drop down populated dynamically

View Answers

October 21, 2010 at 4:26 PM


Hi Friend,

Create table country(country_id,country) in database and try the following code:

<%@page import="java.sql.*"%>
<html>
<form name="form" method="post" >
<b>Select a country:</b>&nbsp;</td>
<select name="sel"><option value=""><---Select---></option>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
String connectionURL = "jdbc:mysql://localhost:3306/test";
Connection connection= DriverManager.getConnection(connectionURL, "root", "root");
PreparedStatement psmnt = connection.prepareStatement("select * from country ");
ResultSet results = psmnt.executeQuery();
while(results.next()){
String name = results.getString(2);
String id = results.getString(1);
%><option value="<%= name %>"><%=name%></option>
<%} results.close(); psmnt.close(); %>
</select><br>
</form>

Thanks


October 21, 2010 at 4:49 PM


Hi Friend,

If you want the code in ajax then try the following 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= "city.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)city.jsp:

 <%@page language="java" import ="java.sql.*" %>  
 <%  
 String country=request.getParameter("count");  
 String buffer="<select name='state'><option value='-1'>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 city where countryid='"+country+"' ");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(2)+"'>"+rs.getString(3)+"</option>";  
   }  
 buffer=buffer+"</select>";  
 response.getWriter().println(buffer);  
 %>

We have used following database tables:

1)country

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

2)city

CREATE TABLE city (
id bigint(40) NOT NULL auto_increment,
countryid int(100) default NULL,
city varchar(40) default NULL,
PRIMARY KEY (id)
)

The above code populate the second dropdown on selecting the value from the first dropdown.

Thanks









Related Pages:
jsp drop down-- select Option
jsp drop down-- select Option  how to get drop down populated...;nbsp;</td> <select name="sel"><option value=""><---Select..."); String buffer="<select name='state'><option value='-1'>Select<
Login With Drop Down
Login With Drop Down   Hi all, I am doing a project using JSP. My... a drop down list consisting of Customs and Accounts. I have user accounts of both the departments. Now, my intention is to select the department from the drop down
dynamic drop down
into the designation drop down (use jsp+javascript+servlet) reply soon   1...dynamic drop down   I have created 2 drop downs in jsp.1...; <select onchange="showDesig(this.value)"> <option value="none
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????  ...("Select * from country"); while(rs.next()){ %> <option value
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>
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
JSP Dependent Drop Down Menu
JSP Dependent Drop Down Menu  Hey Guy/Gals! I need someone help to guide me in creating a drop down menu, where the first menu affects the second... buffer="<select name='state' ><option value='-1'>Select</option>
Dependant & dynamic drop down list
`));   This will select only ONE row (country) from first drop down. In my case...Dependant & dynamic drop down list  I don't know this should be in this or AJAX forum. I have one dynamic drop down list from data base (working
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 page. Consider we have a drop down list which is dynamically displayed using
dynamic drop down list
dynamic drop down list  I want to create 2 drop down list, where it takes value from database and the two list are dependent..means if I select... on the value chosen from the previous. want code in javascript and jsp, Can you help
Drop down combos
Drop down combos  Hi.. How to write a javascript for linked combo...="combo1" onchange="change(this);"> <option value="0">-Select-</option>... language="javascript"> var arr = new Array(); arr[0] = new Array("-select
Dependent drop down list
Dependent drop down list  hi,i am trying to design a form on which 2 dependent drop down list is used my code is successful but when i select class from first drop down list all the data get lossed means the value entered
Login With Drop Down, Having Departments in different table
is to select the department from the drop down box and then login to the page. I... to login when I select department from the drop down list. Since I cannot populate the departments name from each table into drop down I use Select tag and declare
Ajax drop down and textbox
Ajax drop down and textbox  hie frnds I have a dropdown menu in the 3 options(1,2,3) are there if i choose 1 then I should get 1 text box if i select... menu consisting of options 1,2,3. When the user select any option from
Drop down and radio button value on edit action
Drop down and radio button value on edit action  HI, I have a title field and a payment type field for title i have used drop down with values...; by cheque When 'm adding a new customer it works fine as we have to select
Dependant & dynamic drop down list - Follow up
Dependant & dynamic drop down list - Follow up  Thanks for your answer. As per your answer This will select only ONE row (country) from first drop..."); String buffer="<select name='state' ><option value='-1'>Select<
jsp drop down without refresh - JSP-Servlet
jsp drop down without refresh  Sir I have tried two depenedent jsp dropdown in jsp page,But the probleam is when i select first drop down its... jsp same probleam also arise. Please tell me how to use the drop down without
jsp login code ... when username , drop down box and password is correct
jsp login code ... when username , drop down box and password is correct  i need a jsp code for login.... when username password and dropdown box value is correct.... the drop down box values should be retrieved from database
html menu button drop down
html menu button drop down  How to create a menu button in HTML?   <select id="category"> <option value="1">One</option> <option value="2">Two</option> </select> <select id
jsp code for a drop down box to retrive value from database
jsp code for a drop down box to retrive value from database  my project needs to get the value from database in to the drop down box..... pls give me...="pass"></td></tr> <tr><td>Select Company: </td>
Drop down list from database
Drop down list from database  Hi, Can I know how do we get the drop down list from database? Eg: select country--select state--select district--so...; <select name='state' > <option value='-1'><
Drop-down text selection in jsp/html
Drop-down text selection in jsp/html  Hi, I am trying to create JSP page where I have two drop-downs. There are two-sets of data - First set.... Now the first dropdown will have all the values. The second drop-down depends upon
dependent drop down box - JSP-Servlet
dependent drop down box  haloo sir Please give me the solution how to extract data from dependent drop down box with page refresh...; window.location.replace("http://localhost:8080/examples/jsp/dependentDropdown.jsp?id="+cid+"&&value
Select functionality of drop down list - Struts
Select functionality of drop down list  Hi, I have to write a code(in struts application) in which if user select HIDE from the drop down list... for entering Age,Now if user select "HIDE" from drop-down list the below text
Drop Down reload in IE
Drop Down reload in IE  Hi i was using two drop down box..One for Displaying date followed by another for Dispalying Month..If i Select/Change Month from the 2nd drop down then the 1st drop down ( which is date) automatically
how to retrieve the id value itself from the access database to drop down listbox in jsp
access database to drop down list box in jsp...plz send the code for that.....plz   <%@page import="java.sql.*"%> <select> <option value...how to retrieve the id value itself from the access database to drop down
how to set a value of dynamic number of drop down lists on a jsp page and access it value on another jsp page
how to set a value of dynamic number of drop down lists on a jsp page... number of drop down lists(depending upon number of books in that category) on jsp... one setting values of drop down lists and other one accessing it in this jsp
drop down box - JSP-Servlet
drop down box  when i enter some letter in the input box,the corresponding words of that particular letter must be displayed below as a list from the data base in the same input box as drop down. Thanks&Regards, VijayaBabu.M
dynamic drop down list box - Java Beginners
dynamic drop down list box  hi all , I want to dynamically populate a drop down box from an sql query in a servlet program, using only html... User Name: ---Select Name
displaying section in the jsp based on the sected value in drop down - Java Beginners
on the jsp based on the selected value in the drop down list using java script code...displaying section in the jsp based on the sected value in drop down  ...; } Drop Dwon list Select
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
want to insert values in drop down menu in struts1.3
want to insert values in drop down menu in struts1.3  I am using DynaValidatorForm.please help me with inserting values in color drop down menu. I...; <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:fmt="http
Select from drop down and load the appropriate application resource.properties in struts2
Select from drop down and load the appropriate application resource.properties in struts2  I would like to use a dropdown for selecting a language in my application .On selecting a language the appropriate
Get values in drop down list
Get values in drop down list  Pls provide me jsp code to get values in drop down list from another table's field. my project has customer... in drop down box in front end..... pls provide me code.. thanx
data should not repeat in the drop down list when it is loading dynamically from database
data should not repeat in the drop down list when it is loading dynamically... the whole department field from the data base (i.e in the drop down list... be included in the drop down list. 1)selDept.jsp: <%@page import
to update drop down list value when selected from website
to update drop down list value when selected from website  hi help me, i want to insert value into db when i select from a drop down list, this is my code, <div id="age"> Age <select name='NEW'> <option
to update drop down list value when selected from website
to update drop down list value when selected from website  hi help me, i want to insert value into db when i select from a drop down list, this is my code, <div id="age"> Age <select name='NEW'> <option
to update drop down list value when selected from website
to update drop down list value when selected from website  hi help me, i want to insert value into db when i select from a drop down list, this is my code, <div id="age"> Age <select name='NEW'> <option
Drop down for search textbox like google search
Drop down for search textbox like google search  I want drop down like google search (ie, when we type one letter then the word start with that are displayed). when the drop down list appear, then we can select one of word as our
store values of drop down list box in database
store values of drop down list box in database  how to store values of drop down list box in oracle database in jsp?I have information inserting form where i have date of birth as drop down list box
drop down
drop down  how can i add data from choice/dropdown component of java awt to myaql table
Drop Down
Drop Down  How to insert date into database using dropdown like facebook
drop down menu
drop down menu  drop down menu using html
jQuery image replacement by click or select
by button click or by selecting an option of select drop down box without refreshing... to replace image by button click or select drop down without refreshing complete... () { $("select option:selected").each(function () { $("#img").attr
drop down list in Struts
drop down list in Struts  How to create a drop down list in Struts
retrive the data from access database to drop down list box in jsp
retrive the data from access database to drop down list box in jsp  hai, im new to jsp now im using the jsp along with access database.in table i load all the data's i need to retrive the data from database to dropdown list box
view data from database using drop down list
view data from database using drop down list  hi i want to view the data from database by selecting a value in a drop down list. for an example drop down list have picture element.when click it select pictures from the database
How do i retain values in the drop down - Struts
How do i retain values in the drop down  Hi, I have a jsp page... with the drop down. My problem is whenever i do this the values in the drop down... in the drop
validate select option jquery
validate select option jquery  How to Validate select option in JQuery
data should not repeat in the drop down list when it is loading dynamically from database
data should not repeat in the drop down list when it is loading dynamically...; <select name='emp' > <option value='-1'><...; <select name='emp' > <option value='-1'><

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.