Connecting Tech Pros Worldwide Forums | Help | Site Map

How can connect 3 combo box with MYSQL by java script or ASP Classic

Newbie
 
Join Date: Sep 2009
Posts: 3
#1: Sep 1 '09
Hi every body

I would like connect three combo box (CascadingDropDown with a Database) MYSQL by asp classic or Java Script .
But I have a problem with this example , when I will change City , returen the curser to the old select -City .

Can you Solve this problem for me.
Thanks
Expand|Select|Wrap|Line Numbers
  1.  
  2. Const cServer="localhost"
  3. Const cUsername="root"
  4. Const cpassword="123"
  5. const cDatabaseName ="3combo"
  6. Const cPort = "3306"
  7.  
  8. '===========================================
  9. Dim objCon
  10. Dim strDB
  11.  
  12. Sub openDB()
  13.     strDB = "Driver=MySQL ODBC 3.51 Driver;server="&cServer&";uid="&cUsername&";Pwd="&cPassword&";database="&cDatabasename&";Port="&cPort&";Option=147456"
  14.     Set objCon = Server.CreateObject("ADODB.Connection")
  15.     objCon.open strDBEnd Sub
  16.  
  17. Sub closeDB()
  18.     objCon.Close
  19.     Set objCon = Nothing
  20. End sub
  21.  
  22. '========================================
  23. <%@ Language=VBScript %>
  24. <!--#include file="conect.asp"-->
  25.  
  26. %>
  27.  
  28. <html>
  29. <head>
  30. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  31. <title>Example combo box</title>
  32.  
  33. <script language="javascript">
  34. <!--
  35. function dept_onchange(frmSelect) {
  36.     frmSelect.submit(); 
  37. }
  38. //-->
  39. </script>
  40. </head>
  41. <body>
  42. <div align="center">The following was selected : 
  43.  
  44.   <%'=Request.Form ("courses")
  45. Dim X
  46. Dim C
  47. Dim D
  48.  
  49.  
  50. X=Request.Form ("Country2")
  51. C=Request.Form ("City2")
  52. D=Request.Form ("Destric")
  53.  
  54. Response.Write(X)
  55. Response.Write(C)
  56. Response.Write(D)
  57. %>
  58.  
  59. </div>
  60. <form name="frmSelect" method="Post" action="combo test only.asp">
  61. </p>
  62.   </div>
  63.   <table width="270" border="0" align="center" cellpadding="4" cellspacing="4">
  64.     <tr>
  65.       <td><SELECT name=Country2 id="Country2" style="width:144px;" onChange="return dept_onchange(frmSelect)" LANGUAGE=javascript>
  66.         <%
  67. Dim strSQL
  68. Dim RS
  69. strSQL= "SELECT DISTINCT Country FROM asocity ORDER BY ID"
  70. 'strSQL= "SELECT Country FROM asocity group by Country ORDER BY ID"
  71.  
  72. Call openDB
  73. Set RS=objCon.Execute(strSQL)
  74.  
  75. Do while not RS.EOF
  76. '==========================================================================
  77. if Request.Form("Country2") = RS("Country") then
  78.  
  79. Response.Write "<OPTION VALUE = '" & RS ("Country") & "' SELECTED>"
  80. Response.Write RS("Country") & "</Option>"
  81. RS.MoveNext 
  82. else
  83. Response.Write "<OPTION VALUE = '" & RS ("Country") & "'>"
  84. Response.Write RS("Country") & "</Option>"
  85. RS.MoveNext 
  86. end if
  87. loop        
  88. %></SELECT>
  89.         Country</td>
  90.     </tr>
  91.     <tr>
  92.       <td>
  93.  
  94.  
  95.  <SELECT name=City2 id="City2" style="width:144px;" onChange="return dept_onchange(frmSelect)" LANGUAGE=javascript>
  96. <%
  97. Dim strSQL2
  98. Dim RS2
  99. 'strSQL2 = "SELECT city FROM asocity Where City= '"&C&"' group by City"
  100. strSQL2 = "SELECT DISTINCT City FROM asocity Where Country= '"&X&"'"
  101. Call openDB
  102. Set RS2=objCon.Execute(strSQL2)
  103. '============================================
  104. Do while not RS2.EOF
  105. '==================================
  106. Response.write ("<option>" &RS2("City")&"</option>" ) 
  107. RS2.MoveNext
  108. loop
  109.  
  110. 'if Request.Form("City2") = RS2("City") then
  111. 'Response.Write "<OPTION VALUE = '" & RS2 ("City") & "' SELECTED>"
  112. 'Response.Write RS2("City") & "</Option>"
  113. 'RS2.MoveNext 
  114. 'else
  115. 'Response.Write "<OPTION VALUE = '" & RS2 ("City") & "'>"
  116. 'Response.Write RS2("City") & "</Option>"
  117. 'RS2.MoveNext 
  118. 'end if
  119. 'loop        
  120. %>
  121.       </SELECT>
  122.  City</td>
  123.     </tr>
  124.     <tr>
  125.       <td><select name="Destric" style="width:144px;" id="Destric">
  126.         <%
  127. Dim strSQL3
  128. Dim RS3
  129.  
  130. strSQL3 = "SELECT username FROM asocity Where city= '"&C&"'"
  131.  
  132. 'strSQL3 = "SELECT DISTINCT username FROM asocity Where Country= '"&X&"' ORDER BY ID"
  133.  
  134.  
  135. Call openDB
  136. Set RS3=objCon.Execute(strSQL3)
  137.  
  138. Do while not RS3.EOF
  139. Response.write ("<option>" &RS3("username")&"</option>") 
  140. RS3.MoveNext 
  141. loop
  142. %>
  143.       </SELECT>
  144.         Destric</td>
  145.     </tr>
  146.     <tr>
  147.       <td width="254"><input type="submit" name="Submit" value="Add"></td>
  148.     </tr>
  149.   </table>
  150.   <p>&nbsp;</p>
  151.   <p>&nbsp;</p>
  152.  
  153.  
  154.   <div align="center">
  155.     <%
  156.  Dim akm
  157.  akm=Request.Form("txtArea7")
  158.  
  159.  if akm="" then
  160.  akm="0000000000000"
  161.  end if
  162.  
  163.  IF Request.Form("Submit")="Add" then
  164.  Response.write (X)+"<BR>"
  165.  Response.write (C)+"<BR>"
  166.  Response.write (akm) 
  167.  End if
  168.  %>
  169.   </div>
  170. </form>
  171.  
  172.  
  173. </body>
  174. </html>
Attached Files
File Type: zip Exam.zip (10.3 KB, 41 views)

jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#2: Sep 1 '09

re: How can connect 3 combo box with MYSQL by java script or ASP Classic


just trying to make sure I understand you - you say that after the user makes one selection you want to move the cursor to another <select> box?

Jared

BTW, please put your code in [ code] [/ code] tags, it makes it much more readable
Newbie
 
Join Date: Sep 2009
Posts: 3
#3: Sep 1 '09

re: How can connect 3 combo box with MYSQL by java script or ASP Classic


Yes I want move a cursor to select a city , for example when I Select Berlin , in combobox2 , stop the cursor in the Berlin City.
For Example:
Combo box 1 : Country : Germany
Combo box 2 : City : Berlin
Combo box 3: State : G1


For More detail :
When I Select Germany in the Country Combo box 1, Show all City of Germany in the Next Combo box (Combo box2) , and when I Select Berlin in the ( Combo box2) , Return the cursor to Hamborg City ??? I have a problem in (combo box2) ?
Thanks for response us.
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#4: Sep 6 '09

re: How can connect 3 combo box with MYSQL by java script or ASP Classic


OK, this is a fairly common problem, moving the cursor is going to have to be done with javascript, there's no way around that. ASP only works on the server, all ASP code is inactive by the time it gets to the user. The code to move a dursor is going to be something like this:
Expand|Select|Wrap|Line Numbers
  1. <select name="country" onSelect="this.form.city.focus()">
  2. '...
  3. </select>
  4. <select name="city">
Does this make sense? It's been a while since I've done javascript, but it's something like that.

The other part of the question you seem to be asking is how to limit the results of the second select based on the first choice, so if the user chooses "germany" in the first box, he sees "Berlin, Hamburg, etc" in the second. As I mentioned earlier, the ASP code is inactive by the time it gets to the client's browser, so if you want to use ASP to limit the options in the second select box, you need to submit the form (onSelect="this.form.submit()") and then use asp to resend the form with the new data in place in the second box. Does this make sense? If you need help with any part of this task, please ask.

On the other hand, this might slow down the user's experience, and although I've seen plenty of professional sites that use this method, a lot of people use a pure javascript technique wherein all of the possibilities are sent to the user, and after the selection is made a javascript function alters the second select box accordingly. You will have to ask a javascript expert for that method.

There is also one other possibility, the technology "Ajax" was invented to handle exactly this type of issue, and it can communicate back to the server to refresh the list for the second select in real time. This is probably the preferred method nowadays.

Jared
Reply