Dropdown menus onchange atribute

Ads
 

Dropdown menus onchange atribute

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.

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.

Ads









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
ModuleNotFoundError: No module named 'Menus'
ModuleNotFoundError: No module named 'Menus'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'Menus' How to remove the ModuleNotFoundError: No module named 'Menus'
ModuleNotFoundError: No module named 'django-menus'
ModuleNotFoundError: No module named 'django-menus'  Hi, My Python... 'django-menus' How to remove the ModuleNotFoundError: No module named 'django-menus' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'gipsy-menus'
ModuleNotFoundError: No module named 'gipsy-menus'  Hi, My Python... 'gipsy-menus' How to remove the ModuleNotFoundError: No module named 'gipsy-menus' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'Simple-Menus'
ModuleNotFoundError: No module named 'Simple-Menus'  Hi, My Python... 'Simple-Menus' How to remove the ModuleNotFoundError: No module named 'Simple-Menus' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'stwberlin-menus'
ModuleNotFoundError: No module named 'stwberlin-menus'  Hi, My... named 'stwberlin-menus' How to remove the ModuleNotFoundError: No module named 'stwberlin-menus' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-menus'
ModuleNotFoundError: No module named 'django-menus'  Hi, My Python... 'django-menus' How to remove the ModuleNotFoundError: No module named 'django-menus' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'stwberlin-menus'
ModuleNotFoundError: No module named 'stwberlin-menus'  Hi, My... named 'stwberlin-menus' How to remove the ModuleNotFoundError: No module named 'stwberlin-menus' error? Thanks   Hi, In your python
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
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)"> <
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
ModuleNotFoundError: No module named 'djangocms-named-menus'
ModuleNotFoundError: No module named 'djangocms-named-menus'  Hi...: No module named 'djangocms-named-menus' How to remove the ModuleNotFoundError: No module named 'djangocms-named-menus' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-ib-menus'
ModuleNotFoundError: No module named 'django-ib-menus'  Hi, My... named 'django-ib-menus' How to remove the ModuleNotFoundError: No module named 'django-ib-menus' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-cms-named-menus'
ModuleNotFoundError: No module named 'django-cms-named-menus'  Hi...: No module named 'django-cms-named-menus' How to remove the ModuleNotFoundError: No module named 'django-cms-named-menus' error? Thanks   Hi
ModuleNotFoundError: No module named 'gipsy-modeltranslation-menus'
ModuleNotFoundError: No module named 'gipsy-modeltranslation-menus'  ...: No module named 'gipsy-modeltranslation-menus' How to remove the ModuleNotFoundError: No module named 'gipsy-modeltranslation-menus' error
ModuleNotFoundError: No module named 'wagtail-nav-menus'
ModuleNotFoundError: No module named 'wagtail-nav-menus'  Hi, My... named 'wagtail-nav-menus' How to remove the ModuleNotFoundError: No module named 'wagtail-nav-menus' error? Thanks   Hi, In your
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
. 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> <
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
ModuleNotFoundError: No module named 'odoo10-addon-base-onchange-rule'
ModuleNotFoundError: No module named 'odoo10-addon-base-onchange-rule' ...: ModuleNotFoundError: No module named 'odoo10-addon-base-onchange-rule' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-base-onchange-rule' error
ModuleNotFoundError: No module named 'odoo10-addon-onchange-helper'
ModuleNotFoundError: No module named 'odoo10-addon-onchange-helper'  ...: No module named 'odoo10-addon-onchange-helper' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-onchange-helper' error
ModuleNotFoundError: No module named 'odoo10-addon-onchange-helper'
ModuleNotFoundError: No module named 'odoo10-addon-onchange-helper'  ...: No module named 'odoo10-addon-onchange-helper' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-onchange-helper' error
ModuleNotFoundError: No module named 'odoo10-addon-test-onchange-helper'
ModuleNotFoundError: No module named 'odoo10-addon-test-onchange-helper' ...: ModuleNotFoundError: No module named 'odoo10-addon-test-onchange-helper' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-test-onchange-helper'
ModuleNotFoundError: No module named 'odoo10-addon-test-onchange-helper'
ModuleNotFoundError: No module named 'odoo10-addon-test-onchange-helper' ...: ModuleNotFoundError: No module named 'odoo10-addon-test-onchange-helper' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-test-onchange-helper'
ModuleNotFoundError: No module named 'odoo11-addon-onchange-helper'
ModuleNotFoundError: No module named 'odoo11-addon-onchange-helper'  ...: No module named 'odoo11-addon-onchange-helper' How to remove the ModuleNotFoundError: No module named 'odoo11-addon-onchange-helper' error
ModuleNotFoundError: No module named 'odoo11-addon-onchange-helper'
ModuleNotFoundError: No module named 'odoo11-addon-onchange-helper'  ...: No module named 'odoo11-addon-onchange-helper' How to remove the ModuleNotFoundError: No module named 'odoo11-addon-onchange-helper' error
ModuleNotFoundError: No module named 'odoo12-addon-onchange-helper'
ModuleNotFoundError: No module named 'odoo12-addon-onchange-helper'  ...: No module named 'odoo12-addon-onchange-helper' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-onchange-helper' error
ModuleNotFoundError: No module named 'odoo12-addon-onchange-helper'
ModuleNotFoundError: No module named 'odoo12-addon-onchange-helper'  ...: No module named 'odoo12-addon-onchange-helper' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-onchange-helper' error

Ads