The onchange event in Ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SafetyWeek
    New Member
    • Nov 2010
    • 8

    #1

    The onchange event in Ajax

    Hi everyone, I need your help.

    I used the onchange event in select:
    Code:
    <select name="menu" 
    onchange="window.location.href='?code='+this.options[this.selectedIndex].value;">
    <option value="code1">Testo 1</option>

    After loading I can read the value parameter code and show / hide items in relation to that value.

    Now I upgraded to AJAX and I find this situation:
    Code:
    <select name="code" id="code" onChange="cerca_province();">
    <option value="code1">Testo 1</option>
    Code:
    function cerca_province() 
    {
    
    code=document.form.code.options[document.form.code.selectedIndex].value
    
        if (window.XMLHttpRequest) {
            estrai_province= new XMLHttpRequest();
            estrai_province.onreadystatechange = ricevi_province;
            estrai_province.open("GET", "estrai.asp?code="+code, true);
            estrai_province.send(null);
    
        } else if (window.ActiveXObject) {
            estrai_province= new ActiveXObject("Microsoft.XMLHTTP");
            if (estrai_province) {
                estrai_province.onreadystatechange = ricevi_province;
                estrai_province.open("GET", "estrai.asp?code="+code, true);
                estrai_province.send();
    
            }
        }
    }
    
    
      function ricevi_province() {   
        var province;       
    	  if (estrai_province.readyState == 4) {	    
    		  province=estrai_province.responseText;
    
    			document.getElementById('provincia').innerHTML = province;
    			 }
    	}

    But this way I can not read the value of parameter code and show / hide items in relation to that value.


    Can you help me? Can someone help me?
    Thanks in advance.
  • SafetyWeek
    New Member
    • Nov 2010
    • 8

    #2
    I try this but not working:

    Code:
    <select name="codice" id="codice" onChange="cerca_province(this.value);">
    <option value="codice1">Testo 1</option>

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      to use cerca_province( this.value); you have to define a parameter in the function definition:
      Code:
      function cerca_province(val)
      { /* your function code */ }
      PS. and of course you have to use the defined parameter.

      PPS. with proper event handling, you could get the select’s value without further ado through this.value anywhere in the function.

      Comment

      • SafetyWeek
        New Member
        • Nov 2010
        • 8

        #4
        Thanks for your suggestion, but not working...

        In the asp page estrai.asp the value of parameter "codice" is correct but in the this asp page (index.asp) I don't not read the value of parameter "codice".

        I don't receive error... but not read the value of parameter...

        Can someone help me?

        index.asp
        Code:
        <html>
        
        <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
        <script type="text/javascript" language="javascript">
        
        var estrai_province;
        var codice
        
        function cerca_province(codice) 
        {
            if (window.XMLHttpRequest)
            {
                estrai_province = new XMLHttpRequest();
            }
            else if (window.ActiveXObject)
            {
                estrai_province = new ActiveXObject("Microsoft.XMLHTTP");
            }
            
            if (estrai_province)
            {
                estrai_province.onreadystatechange = ricevi_province;
                estrai_province.open("GET", "estrai.asp?codice="+codice, true);
                estrai_province.send(null);
            }
        }
        
          function ricevi_province() {   
            var province;       
        	  if (estrai_province.readyState == 4) {	    
        		  province=estrai_province.responseText;
        
        			document.getElementById('provincia').innerHTML = province;
        			 }
        			 
        	}
           
        </script>
        </head>
        
        
        <body>
        <form method="post" action="" name="form">
        <table>
        
        <%nome_form="form"%>
        
        <tr>
        <td>
        <select name="codice" onChange="cerca_province(this.options[this.selectedIndex].value);">
        <option value="0">Seleziona</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        </select>
        </td>
        </tr>
        	
        <tr>
        <td id="provincia"></td>
        </tr>
        
        </table>   
        
        //// This value not read ////    				
        <% response.write request.querystring("codice") & "<br>" %>
        
        </form>
        
        </body>
        
        </html>

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Thanks for your suggestion, but not working...
          not the way you think …

          given the way AJAX works, why should line #68 return something at all?


          one correction to make: remove var codice, codice is a parameter, not a variable.

          Comment

          • SafetyWeek
            New Member
            • Nov 2010
            • 8

            #6
            OK, I correct the code.

            Link of the page: http://www.romatitlan.com/public/index.asp

            I need read the value of parameter "codice" also on the page index.asp.

            It's possible?
            Thanks.

            Comment

            • SafetyWeek
              New Member
              • Nov 2010
              • 8

              #7
              you can do?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                given your link, the AJAX works without fail.

                Comment

                • SafetyWeek
                  New Member
                  • Nov 2010
                  • 8

                  #9
                  Yes, I have modify the script and I read the value of codice in the index.asp

                  But if I use the codice_value in the server language, for example in ASP query:

                  Code:
                  SELECT * FROM tbl WHERE codice_value = ???
                  What value should I write in this query?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    er, I didn’t understand that question.

                    Comment

                    • SafetyWeek
                      New Member
                      • Nov 2010
                      • 8

                      #11
                      I need execute ASP query using the value of variable "codice".

                      I read in the index.asp the value of variable "codice" with:

                      Code:
                      <span id="codice_value"></span>
                      how do I change this ASP query ? :

                      Code:
                      SELECT * FROM tbl WHERE codice_value = ???
                      Thanks for your attention.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        sorry, but I can’t help you with ASP related questions.

                        Comment

                        Working...