jsp drop down-- select Option

Ads
 

jsp drop down-- select Option

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

Ads









Related Tutorials/Questions & Answers:
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>
Advertisements
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
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
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>
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
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 code for that ..... tan q   1)login.jsp: <html> <script>
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
validate select option jquery
validate select option jquery  How to Validate select option in JQuery
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
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
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
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
How to pass parameter through drop down list using session management in jsp??
How to pass parameter through drop down list using session management in jsp??  How to pass parameter through drop down list in the URL and access it on same jsp page using session management
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
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
drop down menu
drop down menu  drop down menu using html
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
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
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
Pls provide me jsp code to get values in drop down list from another table's field....
Pls provide me jsp code to get values in drop down list from another table's field....  my project has customer registration and company registration... in customer registration page and should be shown in drop down box in front end
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
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
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
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
Drop down menu
Drop down menu  I have drop down list of some 14 links which links to the table,If i click each link the table should display below the link and again if i click the link the table should not appear. please help me
Drop down menu
Drop down menu  I have created a drop down list of links which links to a table but if i click the link the table display in the other page i want to display it in the same page.Please tell me the solution
Drop Down Box
Drop Down Box  In a Drop Down box I want to show the user All the Country In the World. And when he type A-z,then each of the type Show those country which start with those word. Like I For-India
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'><

Ads