View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12762 Members
Join this group to post and comment.
TheV
TheV • Apr 7, 2012

How to Populate DropDown Menu by selecting another DropDown Menu

Hey friends ,
I want to populate the dropdown menu on selecting another dropdown menu. I try javascript but I couldn't do it. What I want to do is there is two dropdown menu, semester and subject. On selecting semester I should get all the subjects of that semester in my subject dropdown menu. Technology: Javascript, jsp, servlet, html
Please help...!
deepu11111111
deepu11111111 • Apr 7, 2012
I am trying
Manish Goyal
Manish Goyal • Apr 8, 2012
This is very simple job
You just have to use ajax for doing this or if you want to do this in Javascript only then you can do this

Add a onChange Event on Semester drop down

and check for ID of semester and append new Options to subjects drop down accordingly
TheV
TheV • Apr 8, 2012
I don't know Ajax at all... 😔 Is there is any nice site to learn from..?
Manish Goyal
Manish Goyal • Apr 8, 2012
Above code will help you in implementing it, there might be some syntax error in this code , as i have not checked it, use firebug in mozilla for checking it
Now Coming to explanation of its working

1:- Now whenever you will select semester
Jquery will capture onChange event and fire ajax to the server and will fetch corresponding subject for the that semester and returns the response
Jquery will then parse that response in success function and fill the corresponding Subject Combo Box
I hope you got an Idea , what i am saying , if you still need details explanation, then take help from google
Just use following query
Populate combo-box box dynamically on Change
You will get thousands of search results
sookie
sookie • Apr 9, 2012
Please check below example how it can be done through JavaScript. In order to get the values for Subjects drop down, you will have to hit the server once or either you can load them during page load itself as I have saved them by "comma separator".




Drop Down Test

Drop Down 1: 


Drop Down 2: 
Let me know if you want to go with Ajax approach.
TheV
TheV • Apr 9, 2012
Actually I wanna do in this way...!!

I want to fetch the values from the database and then assign it to the sub_select (dropdown menu). And the HTML codes are below..: ---


Semester :
TheV
TheV • Apr 9, 2012
I am trying to do in this way but I am not even getting the alert massage .....
sookie
sookie • Apr 10, 2012
TheV
I am trying to do in this way but I am not even getting the alert massage .....
I will correct your code[javascript method : PopulateSubjectMenu()] later but till that time check the JavaScript error in Fire-bug console or IE browser.As far as I know you cannot use JSP scriptlets in JavaScript until or unless it is expression.

You will have to hit the DB using Ajax on change of drop down.
TheV
TheV • Apr 10, 2012
sookie
I will correct your code[javascript method : PopulateSubjectMenu()] later but till that time check the JavaScript error in Fire-bug console or IE browser.As far as I know you cannot use JSP scriptlets in JavaScript until or unless it is expression.

You will have to hit the DB using Ajax on change of drop down.
[INFO] 2012-04-10 11:10:58:840: coupons: warning: strange fullLink=undefined
[INFO] 2012-04-10 11:10:58:840: coupons: warning: strange fullLink=undefined
[INFO] 2012-04-10 11:10:58:840: coupons: warning: strange fullLink=undefined
[INFO] 2012-04-10 11:10:58:840: coupons: warning: strange fullLink=undefined

I am getting the above warning...!!
So Ajax is the last option left . . !
sookie
sookie • Apr 10, 2012
Nope you will get this error, the one I already told you cannot use "scriptlets" in JavaScript
[​IMG]
TheV
TheV • Apr 10, 2012
Ohh...! I think varies from browser to browser..!!
So I have to do using AJAX only..!
sookie
sookie • Apr 10, 2012
Here is your modified code




Semester : Department :
Here is the code for "populateSubjects.jsp" which acts as servlet on ajax call
<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%
            //open a database connection
            Connection conn = null;
            PreparedStatement stmt = null;
            ResultSet rs = null;
            try{
 
              String url = "jdbc:sqlserver://[DB Server IP]:[DB Server Port];";
              String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
              String userName = "[Your Schema username]";
              String password =  [Your Schema pwd] ;
              String semVal=request.getParameter("semVal");
              String deptVal=request.getParameter("deptVal");
           
              Class.forName(driver).newInstance();
              conn = DriverManager.getConnection(url,userName,password);
           
              String s=[FONT=Consolas]"Select sub_code,sub_name from subject_details where semester ="+ [/FONT] semVal [FONT=Consolas]+" and department='"+ [/FONT] deptVal [FONT=Consolas]+"'"[/FONT];
              stmt=conn.prepareStatement(s);
              rs=stmt.executeQuery();
           
              out.print("

Subjects : "); out.print(""); }catch(Exception e ){ out.print("

The following exception was thrown:
"); }finally{ try{if(rs!=null)rs.close();}catch(SQLException e2){/*noop*/} try{if(stmt!=null)stmt.close();}catch(SQLException e2){/*noop*/} try{if(conn!=null)conn.close();}catch(SQLException e2){/*noop*/} } %>
In order to run the above files, Please deploy them in any web container or app server and change the DB connection details to that of yours. Let me know if any issues.

Share this content on your social channels -