Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet Dropdown menus onchange atribute

Have Programming Question? Ask it here!
 
 
 


Amirul Afiq
Dropdown menus onchange atribute
3 Answer(s)      2 years and 3 months ago
Posted in : JSP-Servlet


Can I know how do we get the drop down list from database?

I want to select a type of vehicle from a drop down menu. then based on the selection, it will check with the database with that type of vehicle and populate a drop down menus displaying a list of reg no of that type of vehicle.

for example:
table name: vehicle
type regno
==== =====
bus 12345
bus 35567
van 11111
mini bus 64537

help me please!!!

btw. im using jsp for my code.


Advertisement
View Answers

May 5, 2012 at 11:05 AM


The given code helps us to learn how to retrieve data from database and store it into dropdown list. On selecting the value from the first dropdown, the values will get stored into another dropdown from the 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`));


May 5, 2012 at 11:12 AM


For more information, visit the following link:

http://www.roseindia.net/jsp/comboSelect.shtml

Here you will get an example that will retrieve the data from database and stored it into dropdown list. On selecting the value from the dropdown, respective data will retrieve from database and stored it into textboxes.



May 5, 2012 at 2:17 PM


Thanks. it works great! but, what if i wanted to add another one drop down menu?

in this case, after i chose country, it will show me state corresponds to the country right?

i wanted to add one more drop drop which is city that correspond to the state.



Related Tutorials/Questions & Answers:
Dropdown menus onchange atribute
Dropdown menus onchange atribute  Can I know how do we get the drop... of vehicle and populate a drop down menus displaying a list of reg... from database and store it into dropdown list. On selecting the value from
dropdown
dropdown  how to hide textbox field when i deselect from select dropdown
Advertisements
dropdown
dropdown  I have a dropdown having 2 options-"Open"& "closed".When i select "open" option the related rows of data are retrieved from database and same with the other option.My database is Mysql and coding in PHP
dropdown
; <select name="sel" id="sel" onchange="sel();" > <option value="C">
pull-down navigation menus
pull-down navigation menus  How can I use forms for pull-down navigation menus
Cross frame menus - Ajax
Ragavendran.R.. I need to implement DHTML Cross frame menus..i.e There must be a page with 2 frames(top and bottom).. The main menus must be in the top frame and when I mouse over on it, the corresponding sub menus must be displayed in the bottom frame
populating textbox value from the database using onchange function,
populating textbox value from the database using onchange function,  ... selecting from dropdownlist. i have a form in jsp n I created the dropdown list... downlist it call onchange function with the selected value.. my issue issue
Populate Menus In Tree View - Struts
Populate Menus In Tree View  Hi all, i am writing one simple... the different menus of my application in a tree view.Like what we have in window..." as prefix.In the same way i am thinking to bring the menus in a tree view in my
Create menus and submenus in Java
Create menus and submenus in Java       In this section, you will learn about creation of menus, submenus and Separators in Java Swing. Menu bar contains a collection of menus
DropDown list
DropDown list  how to get mysql database values into dropdown usign... values into dropdown list. 1)country.jsp: <%@page import="java.sql.*"%>...; <select name='country' onchange="showState(this.value)"> <
dropdown box
dropdown box  i need to have country,state and city in drop down box using ajax and use db2 database   Have a look at the following link: JSP dependent dropdown
dynamic creation of multiple drop-down menus
dynamic creation of multiple drop-down menus  can anybody plz help me in coding for dynamic creation of multiple drop-down menus in a webpage using ajax/js/html/css
Ajax Dropdown
Ajax Dropdown  hi I have One Dropdown that contains 2 options assume A and B,if i select A option then samepage one more Dropdown is their it should display values in german language using DWRUtil Parameter values in ajax. Only
dropdown cacheing
dropdown cacheing   I need code to create cache for load drop down list from db in struts1 example
DropDown Menu
DropDown Menu  Hello, i have a program that can view,add,delete... me draw to your my mind my program.. in my homepage which i have a dropdown... is the problem.. for example; if i select page 1 in my dropdown menu, and click
Menus
. There are several types. Dropdown menus hang down from the menu bar... and radio buttons. Dropdown menus: JMenuBar, JMenu, and JMenuItem..., JApplet, or JDialog. Note that a menu bar can not be added to JPanel. Dropdown menus
. Display JavaScript dropdown selected value
="dropdown" onchange="selectDropdown()"> <option value="India" >India<.... Display JavaScript dropdown selected value  How to get the selected value from dropdown box? I am using javascript.   <html> <
Select DropDown Default
Select DropDown Default  Hi there, i have a program in JSP where i... of the record, in my jsp page i have a one dropdown menu where the user can choose, i.e if the user select PAGE 1 in the dropdown menu, it will display all
JSF imple using menus,header,body - Java Server Faces Questions
JSF imple using menus,header,body  Hi All, Could you please provied an examples of JSF implementation of Menus(alist two menusitmes),header,body and footer.   Hi Friend, Please visit the following link
Retrieval of Dropdown list
Retrieval of Dropdown list  Hi frnds... Am having problem... ... for eg, let A1,A2,A3,A4 be the values in dropdown.. if i click A1... dropdown.. Am not able to retrive the name for further process... Pls check
Dependant Dropdown Lists
Dependant Dropdown Lists  Hello, I'm trying to create 2 dropdown lists. When the visitor chooses one option from the 1st list it will automatically update the 2nd dropdown. Ive already found some code which i edited but still
creating dropdown lists in jsp
creating dropdown lists in jsp  i want to create two dropdown list which are dependent that is the first box choice have to evaluate the second boxs options
html dropdown not working firefox
html dropdown not working firefox  I am writing a Dropdown code in HTML which is not working in firefox. What could be the reason as it's perfectly working in IE and Crome. Thanks
dropdown list in jsp
dropdown list in jsp  hai, i have static dropdown list.. i want to get the selected value in string variable without jsp regards asha
DropDown in ajax+jsp
DropDown in ajax+jsp  I have four dropdown if i select first dd...; <select name='country' onchange="showState(this.value)">...='state' onchange='showCity(this.value);'><option value='-1'>Select<
ajax+dropdown+table format
ajax+dropdown+table format  HI, i am looking for an ajax application which must contain two dropdown list.In first dropdown if i select a manager name from the first dropdown all the reporties under the selected manager should
populate dropdown box
populate dropdown box  hi, Is there any ways in which i can populate my dropdown menu with values from 1 to n (n = the value stored in database... item in dropdown menu is 10 then the second dropdown box should contain values
fetch values in dropdown
fetch values in dropdown  in my application i want fetch dropdown values in jsp page through servlet. means i have to fetch the database fields values in array variable of servlet and then i have to print those values in dropdown
parent and child dropdown
parent and child dropdown  Hi, I have one scenario in spring,i need to populate child drop down,based on parent dropdown using spring mvc and ajax... know how to use it in child drop down.How to set that child dropdown in success
Menus
Menus       We can also develop an application with a Menu. As a name indicates a Menu consists..., separator, menu etc. Following are the steps to to add menus to any Frame: You
struts dropdown list
struts dropdown list   In strtus how to set the dropdown list values from database ? I have a ArrayList object and set this object in dropdown jsp page using struts ? please send me jsp code... sample code: **Action
dropdown list in jsf
dropdown list in jsf  I want to add a list box to display the country name from the lists on all countries.When I select for e.g India then in the second list box it will display the states related to India only and the flow
checkbox with dropdown listbox in jsp
checkbox with dropdown listbox in jsp  i loaded the the data into the drop down listbox in jsp from access database.now i have to add a check box,if it checked means dropdownlist box carryout the data's,else it should
Hash Table for maintaining popup menus in Java
Hash Table for maintaining popup menus in Java  ... of maintaining popup menus through hash table in your Java program. This section... to create and maintain popup menus in Java. All the elements listed in the hash
jsp- database dependent dropdown list
jsp- database dependent dropdown list   i want 2 dropdown list 1... suppose class a the corresponding section dropdown list should retrieve the section...' onchange="showState(this.value)"> <option value="none">
inserting dropdown values into database table
inserting dropdown values into database table   hi i want to insert dropdown values into a database table by using jsp
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 event without using from submission   i have 4 check box name 1)profile 2)commercial 3)motion 4)personal and 4 radio button name 1)500-1000 2
Label n Dropdown Issue
Label n Dropdown Issue  Hi guys, Some how Iam able to receive the data for labels, dropdowns. I created one array with the size of label... with corresponding labels... the last dropdown is overriding the previous one... below
Dropdown and label issue
Dropdown and label issue  This is Venkat from Hyderabad,India.Working as a JavaDeveloper herein. Could you please read the code n give your valuable... to display the dropdowns with corresponding labels... the last dropdown
jsp dropdown - JSP-Servlet
jsp dropdown  in my table there are 3 fields named orderid ,itemname, itemqty. i want code that fetch orderid in dropdown... on select dropdown get all items list of that orderid... there are multiple items at one orderid
dropdown in struts - Struts
dropdown in struts  how to populate a drop down from database entries in struts application when i have the workflow as jsp->acton->Business Delegator->controller->Business object->DAO->database. Where should i
dropdown - JSP-Servlet
dropdown  i have below code for dropdown . now i want to user that textbox value in query at same page... how to get it in a new variable??? function getValue(){ var combo = document.getElementById("sel"); var val
dropdown - JSP-Servlet
dropdown  i want to use dropdown select value in query-string in same page.. Using selected 'name' want to fetch details of that person from database.. without using Ajax...  Hi Friend, Try the following code
multiple dropdown issue
multiple dropdown issue  This is Venkat from Hyderabad,India.Working... was not able to display the dropdowns with corresponding labels... the last dropdown... variables, and submit, then the last variable and its corresponding dropdown
jsp dropdown - JSP-Servlet
jsp dropdown  print dropdown selected value in same page without refreshing page in jsp....   Hi Friend, Try the following code: 1)selectbox.jsp: var xmlHttp function showState(str
DropDown Department Login
DropDown Department Login  Hi all, I am doing a project using JSP. My Issue is..... I have two departments name Customs and Accounts in the database with different tables. I have designed the HTML page in such a way the I have
dropdown - JSP-Servlet
dropdown  i want to use dropdown select value in query-string in same page.. Using selected 'name' want to fetch details of that person from database..  Hi Friend, Try the following code: 1)ajaxExample.jsp
binding data with textbox on dropdown click
binding data with textbox on dropdown click  Hello friends,Divyesh here. i have jsp page and i would like to bind data with text box on dropdown click. in dropdown employee id are load.and when we select perticular id than how
how to set value of dropdown list
how to set value of dropdown list  Hello Sir, I'd solved the earlier problems somehow but now this time another problem arised when I want to pass the value from a link to a dropdown list. I want to set the dropdown list selected
how to set value of dropdown list
how to set value of dropdown list  Hello Sir, I'd solved the earlier problems somehow but now this time another problem arised when I want to pass the value from a link to a dropdown list. I want to set the dropdown list selected
 

 

 

DMCA.com