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...!

Replies

  • deepu11111111
    deepu11111111
    I am trying
  • Manish Goyal
    Manish Goyal
    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
    I don't know Ajax at all... 😔 Is there is any nice site to learn from..?
  • Manish Goyal
    Manish Goyal
    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
    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
    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
    I am trying to do in this way but I am not even getting the alert massage .....
  • sookie
    sookie
    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
    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
    Nope you will get this error, the one I already told you cannot use "scriptlets" in JavaScript
    [​IMG]
  • TheV
    TheV
    Ohh...! I think varies from browser to browser..!!
    So I have to do using AJAX only..!
  • sookie
    sookie
    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.

You are reading an archived discussion.

Related Posts

Arstechnica's Ryan Paul spent two days with Facebook release engineering team's Chuck Rossi. Rossi heads the release engineering team at Facebook and is responsible for making the updates go live...
Good Day all Engineers This my first Post in this Forums Introduction to MATLAB For student EngineerThis is a great book for every student who want to "starts" with MATLAB...
Name: Pradeep Chandra Maddirala *Engineering Trade: M.Tech CSE Location: Guntur Andhra Pradesh *Occupation: Student Work Experience: No *Hobbies & Interests: I am interested in cricket , reading threads in net...
I wanna buy an electronic music keyboard of Yamaha or Casio for a reasonable prize Can you help me in which city and area i can get it . here...
Xavier College Prep World History teacher Ms. Nunez will have to miss class during the critical review week before final exams. She turns to Google to interact and connect with...