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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alandiit
    New Member
    • Aug 2009
    • 3

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

    Hi every body

    I would like connect three combo box (CascadingDropDo wn 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
    Code:
    Const cServer="localhost"
    Const cUsername="root"
    Const cpassword="123"
    const cDatabaseName ="3combo"
    Const cPort = "3306"
    
    '===========================================
    Dim objCon
    Dim strDB
    
    Sub openDB()
    	strDB = "Driver=MySQL ODBC 3.51 Driver;server="&cServer&";uid="&cUsername&";Pwd="&cPassword&";database="&cDatabasename&";Port="&cPort&";Option=147456"
    	Set objCon = Server.CreateObject("ADODB.Connection")
    	[B]objCon.open strDB[/B]End Sub
    
    Sub closeDB()
        objCon.Close
    	Set objCon = Nothing
    End sub
    
    '========================================
    <%@ Language=VBScript %>
    <!--#include file="conect.asp"-->
    
    %>
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Example combo box</title>
    
    <script language="javascript">
    <!--
    function dept_onchange(frmSelect) {
    	frmSelect.submit(); 
    }
    //-->
    </script>
    </head>
    <body>
    <div align="center">The following was selected : 
    
      <%'=Request.Form ("courses")
    Dim X
    Dim C
    Dim D
    
    
    X=Request.Form ("Country2")
    C=Request.Form ("City2")
    D=Request.Form ("Destric")
    
    Response.Write(X)
    Response.Write(C)
    Response.Write(D)
    %>
    
    </div>
    <form name="frmSelect" method="Post" action="combo test only.asp">
    </p>
      </div>
      <table width="270" border="0" align="center" cellpadding="4" cellspacing="4">
        <tr>
          <td><SELECT name=Country2 id="Country2" style="width:144px;" onChange="return dept_onchange(frmSelect)" LANGUAGE=javascript>
            <%
    Dim strSQL
    Dim RS
    strSQL= "SELECT DISTINCT Country FROM asocity ORDER BY ID"
    'strSQL= "SELECT Country FROM asocity group by Country ORDER BY ID"
    
    Call openDB
    Set RS=objCon.Execute(strSQL)
    
    Do while not RS.EOF
    '==========================================================================
    if Request.Form("Country2") = RS("Country") then
    
    Response.Write "<OPTION VALUE = '" & RS ("Country") & "' SELECTED>"
    Response.Write RS("Country") & "</Option>"
    RS.MoveNext 
    else
    Response.Write "<OPTION VALUE = '" & RS ("Country") & "'>"
    Response.Write RS("Country") & "</Option>"
    RS.MoveNext 
    end if
    loop		
    %></SELECT>
            Country</td>
        </tr>
        <tr>
          <td>
    
          
     <SELECT name=City2 id="City2" style="width:144px;" onChange="return dept_onchange(frmSelect)" LANGUAGE=javascript>
    <%
    Dim strSQL2
    Dim RS2
    [B]'strSQL2 = "SELECT city FROM asocity Where City= '"&C&"' group by City"
    strSQL2 = "SELECT DISTINCT City FROM asocity Where Country= '"&X&"'"[/B]
    Call openDB
    Set RS2=objCon.Execute(strSQL2)
    '============================================
    [B]Do while not RS2.EOF
    '==================================
    Response.write ("<option>" &RS2("City")&"</option>" ) 
    RS2.MoveNext
    loop[/B]
    
    'if Request.Form("City2") = RS2("City") then
    'Response.Write "<OPTION VALUE = '" & RS2 ("City") & "' SELECTED>"
    'Response.Write RS2("City") & "</Option>"
    'RS2.MoveNext 
    'else
    'Response.Write "<OPTION VALUE = '" & RS2 ("City") & "'>"
    'Response.Write RS2("City") & "</Option>"
    'RS2.MoveNext 
    'end if
    'loop		
    %>
          </SELECT>
     City</td>
        </tr>
        <tr>
          <td><select name="Destric" style="width:144px;" id="Destric">
            <%
    Dim strSQL3
    Dim RS3
    
    strSQL3 = "SELECT username FROM asocity Where city= '"&C&"'"
    
    'strSQL3 = "SELECT DISTINCT username FROM asocity Where Country= '"&X&"' ORDER BY ID"
    
    
    Call openDB
    Set RS3=objCon.Execute(strSQL3)
    
    Do while not RS3.EOF
    Response.write ("<option>" &RS3("username")&"</option>") 
    RS3.MoveNext 
    loop
    %>
          </SELECT>
            Destric</td>
        </tr>
        <tr>
          <td width="254"><input type="submit" name="Submit" value="Add"></td>
        </tr>
      </table>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      
        
      <div align="center">
        <%
     Dim akm
     akm=Request.Form("txtArea7")
     
     if akm="" then
     akm="0000000000000"
     end if
     
     IF Request.Form("Submit")="Add" then
     Response.write (X)+"<BR>"
     Response.write (C)+"<BR>"
     Response.write (akm) 
     End if
     %>
      </div>
    </form>
    
    
    </body>
    </html>
    Attached Files
    Last edited by jhardman; Sep 1 '09, 06:41 PM. Reason: added code tags - please note button marked #
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    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

    Comment

    • alandiit
      New Member
      • Aug 2009
      • 3

      #3
      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.

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        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:
        Code:
        <select name="country" onSelect="this.form.city.focus()">
        '...
        </select>
        <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

        Comment

        Working...