Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic select with ajax and asp

    Hello to all

    I'm trying to modify a script ajax / asp that I found on the net, but I have some difficulty with ajax.

    Essentially these are three select concatenate (originally 2) Regions, provinces and municipalities.
    I can not filter from province to municipality.



    The scripts are:

    form.asp (contiene config.asp e italia.js)
    config.asp (file di connessione)
    italia.js (richiama gli script province e comuni)
    province.asp
    comuni.asp


    FORM.ASP

    <%@LANGUAGE = VBScript%>
    <!--#include file="config.asp"-->
    <%
    Call CnOpen()
    %>
    <html>
    <head>
    <title>Selezione di Regioni e Province con ASP ed AJAX</title>
    <script type="text/javascript" src="italia.js"></script>
    </head>
    <body>

    <form name="italia">

    <select name="regioni" onchange="Province(this.value)">
    <option value="0"></option>
    <%
    Dim SQL
    SQL = "SELECT * FROM regioni ORDER BY reg_nome ASC"
    rs.Open SQL, cn, 1
    While rs.EOF = False
    %>
    <option value="<%=rs("reg_id")%>"><%=rs("reg_nome")%></option>
    <%
    rs.MoveNext
    Wend
    rs.Close
    %>
    </select>

    <br><br>
    <div id="risultati">
    <select name="province" onchange="Comuni(this.value)">
    <option value="0"></option>
    <%
    Dim SQL1
    SQL1 = "SELECT * FROM province ORDER BY pro_nome ASC"
    rs.Open SQL1, cn, 1
    While rs.EOF = False
    %>
    <option value="<%=rs("pro_id")%>"><%=rs("pro_nome")%></option>
    <%
    rs.MoveNext
    Wend
    rs.Close
    %>
    </select>

    <select name="comuni" >
    <option value="0"></option>
    <%
    Dim SQL2
    SQL2 = "SELECT * FROM comuni ORDER BY com_nome ASC"
    rs.Open SQL2, cn, 1
    While rs.EOF = False
    %>
    <option value="<%=rs("com_id")%>"><%=rs("com_nome")%></option>
    <%
    rs.MoveNext
    Wend
    rs.Close
    %>
    </select>

    </div>

    </form>

    </body>
    </html>
    <%
    Call CnClose()
    %>





    ITALIA.JS


    var XMLHTTP;

    function Province(ID)
    {
    if (parseInt(ID) > 0)
    {
    var url = "province.asp?id=" + ID;
    XMLHTTP = RicavaBrowser(CambioStato);
    XMLHTTP.open("GET", url, true);
    XMLHTTP.send(null);
    }
    else
    {
    document.getElementById("risultati").innerHTML = "";
    }
    }

    function Comuni(ID)
    {
    if (parseInt(ID) > 0)
    {
    var url = "comuni.asp?id=" + ID;
    XMLHTTP = RicavaBrowser(CambioStato);
    XMLHTTP.open("GET", url, true);
    XMLHTTP.send(null);
    }
    else
    {
    document.getElementById("risultati").innerHTML = "";
    }
    }


    function CambioStato()
    {
    if (XMLHTTP.readyState == 4)
    {
    var R = document.getElementById("risultati");
    R.innerHTML = XMLHTTP.responseText;
    }
    }


    function RicavaBrowser(QualeBrowser)
    {
    if (navigator.userAgent.indexOf("MSIE") != (-1))
    {
    var Classe = "Msxml2.XMLHTTP";
    if (navigator.appVersion.indexOf("MSIE 5.5") != (-1));
    {
    Classe = "Microsoft.XMLHTTP";
    }
    try
    {
    OggettoXMLHTTP = new ActiveXObject(Classe);
    OggettoXMLHTTP.onreadystatechange = QualeBrowser;
    return OggettoXMLHTTP;
    }
    catch(e)
    {
    alert("Errore: l'ActiveX non verrà eseguito!");
    }
    }
    else if (navigator.userAgent.indexOf("Mozilla") != (-1))
    {
    OggettoXMLHTTP = new XMLHttpRequest();
    OggettoXMLHTTP.onload = QualeBrowser;
    OggettoXMLHTTP.onerror = QualeBrowser;
    return OggettoXMLHTTP;
    }
    else
    {
    alert("L'esempio non funziona con altri browser!");
    }
    }





    PROVINCE.ASP



    <%@LANGUAGE = VBScript%>
    <!--#include file="config.asp"-->
    <%
    Dim id
    id = Request.QueryString("id")
    If IsNumeric(id) = False Or id < 1 Then
    Response.Write "Selezionare una regione valida!"
    Response.End
    End If
    Call CnOpen()
    Dim SQL
    SQL = "SELECT * FROM province "
    SQL = SQL & "WHERE pro_regione = " & id & " "
    SQL = SQL & "ORDER BY pro_nome ASC"
    rs.Open SQL, cn, 1
    If rs.EOF Then
    Response.Write "Selezionare una regione valida!"
    Else
    %>
    <select name="regioni">
    <option value="0"></option>
    <%
    While rs.EOF = False
    %>
    <option value="<%=rs("pro_id")%>"><%=rs("pro_nome")%></option>
    <%
    rs.MoveNext
    Wend
    %>
    </select>
    <%
    End If
    rs.Close
    Call CnClose()
    %>






    COMUNI.ASP

    <%@LANGUAGE = VBScript%>
    <!--#include file="config.asp"-->
    <%
    Dim id
    id = Request.QueryString("id")
    If IsNumeric(id) = False Or id < 1 Then
    Response.Write "Selezionare una provincia valida!"
    Response.End
    End If
    Call CnOpen()
    Dim SQL
    SQL = "SELECT * FROM comuni "
    SQL = SQL & "WHERE com_provincia = " & id & " "
    SQL = SQL & "ORDER BY com_nome ASC"
    'Response.Write sql
    'Response.end

    rs.Open SQL, cn, 1
    If rs.EOF Then
    Response.Write "Selezionare una provincia valida!"
    Else
    %>
    <select name="province">
    <option value="0"></option>
    <%
    While rs.EOF = False
    %>
    <option value="<%=rs("com_id")%>"><%=rs("com_nome")%></option>
    <%
    rs.MoveNext
    Wend
    %>
    </select>
    <%
    End If
    rs.Close
    Call CnClose()
    %>



    I think the problem is in ajax.

    Can you help me?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You really should find different XMLHttpRequest code. Sniffing browser agents is BAD.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •