#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    39
    Rep Power
    4

    Dependant drop down box


    Hi..

    [asp/vbscript/javascript]

    I am using dependent dropdown box in my program..
    If i select the value in the first drop down..based on the value fields in the second drop down should change using databse.



    first drop down: country
    second drop down:state
    Based on country the values in the second drop down should display the states.


    can anyone know how to use dependant drop down.. plese give me an example..


    Thanks..
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    724
    Rep Power
    291
    You couldn't get jQuery to work for you?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    39
    Rep Power
    4
    No I couldn't get the result..

    I am trying the various ways...

    If anyone know plz give me an example ...
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    724
    Rep Power
    291
    You're going to need javascript to do it.

    I'll see if I can mock one up for you tonight using jQuery if you like.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    39
    Rep Power
    4
    This is the code i am using..
    But there is wrong result it is showing..When you select an item in first dropdown the values are changing in the second drop down..this one working fine for first time...

    If I change the values in the first drop down at second time (without refreshing the browser), the second drop down values are changing and the same time previous values are appending..

    The out put is not I expected..

    Code:
    <%
    OpenSQLConn	
    
    if request("submit") <> "" then
    			
    		
    		listbox1=Request.form("listbox1")
      		listbox2=Request.form("listbox2")
    		sql1="select * from master where menu_id='"& listbox1 &"'"
    		set rs=Conn.execute(sql1)
    		'response.write("afterif")
    		
    %>	 
    <table border="1">	
    <tr>
         <th>Menu Type</th>
         <th>Size</th>
         <th>Itemid</th>
          <th>Quantity</th>
    </tr>
    	<%
    		do until rs.EOF
       		response.write("<tr>")
       		response.write("<td>" & rs("menu_id") & "</td>")
       		response.write("<td>" & rs("sizeid") & "</td>")
       		response.write("<td>" & rs("itemid") & "</td>")
       		response.write("<td>" & rs("quantity") & "</td>")
       		  
       		response.write("</tr>")
       		rs.MoveNext
    		loop
    		rs.close
    		Conn.Close
    
    	%>
    </table>
    
    <%		
    	end if
     %>	
    
    
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Dependent Dropdownlists</title>
    
    
    <%
    Dim myRS 'Object
    Dim mySQL 'String
    Dim StartAuthor 'String
    Dim Cnt 'Integer
    Dim ListBox1 'String
    
    
    %>
    
    <script language="javascript">
    function listboxchange(p_index)
    {
    <%
    mySQL = "SELECT * FROM menu Order By menuid "
    
    'Get Recordset using our SQL
    Set myRS = Conn.Execute(mySQL) 
    %>
    switch (p_index)
    {
    <%
    'Loop Through all authors and books
    Do Until myRS.eof 
    StartAuthor = myRS("menuid")
    Cnt = 0
    
    %>
    case "<%=StartAuthor%>" :
    <% 
    
    do until myrs.eof or StartAuthor <> myRS("menuid")
    %>
    document.myform.listbox2.options[<%=cnt%>]=new Option("<%=myRs("sizeid")%>","<%=myRs("size")%>");
    <%
    Cnt = Cnt + 1
    myrs.movenext
    if myrs.eof then exit do
    Loop
    %>
    break;
    <%
    Loop
    
    %>
    }
    return true;
    }
    </script>
    
    
    <%
    mySQL = "SELECT DISTINCT menuid FROM menu Order By menuid asc"
    
    
    Set myRS = Conn.Execute(mySQL) 
    
    'Make Drop down box Author list 
    do until myrs.eof
    ListBox1 = ListBox1 & "<option value=""" & myRs("menuid") & """>" & myRs("menuid") & "</option>"
    myRs.movenext
    loop
    
    myRS.close
    Set myRS = nothing 
    Conn.close
    
    %>
    </head>
    <body>
    <form name="myform" action="" method="get">
    <select name="listbox1" id="listbox1" onChange="javascript: listboxchange(this.options[this.selectedIndex].value);">
    <option value="">Please Select</option>
    <%=ListBox1%>
    </select>
    
    <select name="listbox2">
    <option value="">Please Select</option>
    </select>
    <input type="submit" name="submit" value="show values" />
    </form>
    
    
    </body>
    </html>


    [MYSQL]Sql table:
    menu table[menuid(pk),menu]
    size table[sizeid(pk),size]
    master table[menu_id(fk),sizeid(fk),menu_type][/MYSQL]
    my desired out put is
    menuid,sizeid,size


    Can any one find out the error..and the same time i didn't understand the code much..
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    724
    Rep Power
    291
    Change the following line from this
    Code:
    sql1="select * from master where menu_id='"& listbox1 &"'"
    to this
    Code:
    sql1="select menu_id,sizeid,size from master m join size s on s.sizeid = m.sizeid where menu_id='"& listbox1 &"'"

    As a side note, I see that you are getting the quantity but I see no quantity in any of your tables.

    I don't think it'll work the way you intend for it too. Try using the script I gave you and comment out the quantity for now and see if that works.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    39
    Rep Power
    4
    It is displying which is not necessary..

    first dropdown i selected 101 and
    second dropdown i selected size as 'S'

    It should display the output
    101 s

    but it is displaying
    101 S
    101 M.. both.

    here the second dropdown is not working..
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    39
    Rep Power
    4
    Do I need to pass two values(listbox1 and listbox2) in sql query..
    like as you mentioned in that..
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    724
    Rep Power
    291
    Yes, you will need to pass both.

    Code:
    sql1="select menu_id,sizeid,size from master m join size s on s.sizeid = m.sizeid where menu_id='"& listbox1 &"'" and s.sizeid = '" & listbox2 & "'"
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    39
    Rep Power
    4
    Got it..Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo