SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 29

Thread: Onchange event

  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Onchange event

    Hi everyone, I need your help.

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

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

    Now I upgraded to AJAX and I find this situation:
    Code:
    <select name="codice" id="codice" onChange="cerca_province();">
    <option value="codice1">Testo 1</option>
    Code:
    function cerca_province() 
    {
    
    codice=document.form.codice.options[document.form.codice.selectedIndex].value
    
        if (window.XMLHttpRequest) {
            estrai_province= new XMLHttpRequest();
            estrai_province.onreadystatechange = ricevi_province;
            estrai_province.open("GET", "estrai.asp?codice="+codice, 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?codice="+codice, 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 "codice" and show / hide items in relation to that value.


    Can you help me? Can someone help me?
    Thanks in advance.

  2. #2
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    local variable
    Code:
    var codice=document.form.codice.options[document.form.codice.selectedIndex].value
    or pass the value
    Code:
    <select name="codice" id="codice" onchange="cerca_province(this.value);">

  3. #3
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dogFang View Post
    local variable
    Code:
    var codice=document.form.codice.options[document.form.codice.selectedIndex].value
    or pass the value
    Code:
    <select name="codice" id="codice" onchange="cerca_province(this.value);">
    Thanks for your help but your suggestion not working...
    I can not read the value of parameter "codice"...


  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try this, i cleaned up your code as repeating your ajax call 2 times is redundant

    HTML Code:
    <select name="codice" id="codice" onChange="cerca_province(this.value);">
        <option value="codice1">Testo 1</option>
    </select>
    Code JavaScript:
    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);
        }
    }

  5. #5
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks but not working... I can not read the value of parameter "codice"...

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The code should work perfectly fine as I've used it successfully heaps of times.

    Are you getting any errors?
    What exactly happens when you select a new option value?

  7. #7
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok.

    I don't receive error... no value to the parameter...

    index.asp
    Code:
    <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" id="codice" onChange="cerca_province(this.value);">
    <option value="0">Seleziona DTR</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    </select>
    </td>
    
    <td id="provincia"></td>
    
    </tr>	
    
    </table>   
        		
    </form>
    			
    // This code not working //			
    <% response.write request.querystring("codice") & "<br>" %>
    
    </body>
    
    </html>

  8. #8
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Do you reach readystate 4?
    If your debugger shows you at 0, then you're trying to do it locally (which for some reason doesn't work). The page with the Javascript has to be on the server with the asp script.

    If you're getting readystate 1, then for some reason the onreadystatechange function is running before the .open and .send (I get that a lot and still haven't figured out why).

    Code:
      function ricevi_province() {   
        var province;       
    	  if (estrai_province.readyState == 4) {
                alert("READYSTATE 4 !!!");	    
              }
      }

  9. #9
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The page with the Javascript are on the server with the asp script.
    Really I don't realize what mistakes...

  10. #10
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Javascript and ASP

    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.
    Last edited by Mittineague; Dec 8, 2010 at 00:43.

  11. #11
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try this

    HTML Code:
    This is the asp page index.asp.<br>Value of codice = <span id="codice_value"></span>
    Code JavaScript:
    function cerca_province(codice)  
    {
        // Set the value of "codice"
        document.getElementById('codice_value').innerHTML = codice;
     
        // Make a new Ajax HTTP Request
        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); 
        } 
    }

  12. #12
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Many thanks, your suggestion it's right.

    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?

  13. #13
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  14. #14
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ASP runs on the server, and runs BEFORE any HTML/Javascript gets executed. You'll need to use a method to submit the value to index.asp, e.g. as a hidden form field value, so that ASP can get the value as normal ...

    codice_value = Request.Form("codice_value")
    Ian Anderson
    www.siteguru.co.uk

  15. #15
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    ASP runs on the server, and runs BEFORE any HTML/Javascript gets executed. You'll need to use a method to submit the value to index.asp, e.g. as a hidden form field value, so that ASP can get the value as normal ...

    codice_value = Request.Form("codice_value")
    Many thanks for your help, but I don't understand your suggestion.
    The value get in a hidden form field value is null.

    This is page index.asp:

    Code:
    <html> 
      
    <head> 
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 
    <script type="text/javascript" language="javascript"> 
      
    var estrai; 
      
    function cerca(codice)  
    {
        document.getElementById('codice_value').innerHTML = codice;
        
        if (window.XMLHttpRequest) 
        { 
            estrai = new XMLHttpRequest(); 
        } 
        else if (window.ActiveXObject) 
        { 
            estrai = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
      
        if (estrai) 
        { 
            estrai.onreadystatechange = ricevi; 
            estrai.open("GET", "estrai.asp?codice="+codice, true); 
            estrai.send(null); 
        } 
    }
      
      function ricevi() {    
        var xxx;        
          if (estrai.readyState == 4) {         
              xxx=estrai.responseText; 
      
                document.getElementById('xxx').innerHTML = xxx; 
                 } 
      
        } 
      
    </script> 
    </head> 
      
      
    <body> 
    <form method="post" action="" name="form"> 
    <table> 
      
     
      
    <tr> 
    <td> 
    <select name="codice" onChange="cerca(this.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="xxx">This is the page estrai.asp... </td> 
    </tr>
    
    <tr> 
    <td id="codice_value">This is page index.asp... </span></td> 
    </tr>
    
    </table>  
     
    <input type="hidden" value=document.form.codice.value name="codice_value" />
     
    </form> 
     
     
    <br>
     
    <% 
    
          SQL = " SELECT SQL_CALC_FOUND_ROWS * FROM "
          SQL = SQL & " tbl "
          SQL = SQL & " WHERE 1 "
          SQL = SQL & " AND ID = " & Request.Form("codice_value") & " "
          
          response.write SQL
    
    %>  
    
    
      
    </body> 
      
    </html>
    estrai.asp
    Code:
    <% 
       stringa = request.querystring("codice")
       response.write stringa & "<br>"
    %>

  16. #16
    Non-Member
    Join Date
    Jul 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Guys
    VBScript is the default scripting language that ASP is coded in, so if you want to specify a different scripting language you have to state which scripting language you will be using at the very beginning of your code. Below is the line of code that must be your first line of ASP code or else your page will break and you'll get a boring error message.

  17. #17
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r.pointing View Post
    Hello Guys
    VBScript is the default scripting language that ASP is coded in, so if you want to specify a different scripting language you have to state which scripting language you will be using at the very beginning of your code. Below is the line of code that must be your first line of ASP code or else your page will break and you'll get a boring error message.
    Sorry... for me it's difficult to understand...

  18. #18
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It was a slightly misleading reply ... I can see that you are using AJAX to send info to an ASP page and then use the response.

    Perhaps the following test code might help in your investigation?
    Code:
    function cerca (codice) {
    	var xmlHttp;
    	var resp = '';
    	try {
    		// Firefox, Opera 8.0+, Safari
    		xmlHttp = new XMLHttpRequest();
    	}
    	catch (e) {
    		// Internet Explorer
    		try {
    			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch (e) {
    			try {
    				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch (e) {
    				alert ('Your browser does not handle AJAX requests');
    				return false;
    			}
    		}
    	}
    	xmlHttp.onreadystatechange = function() {
    		if (xmlHttp.readyState == 4) {
    			resp = xmlHttp.responseText;
    			if (resp != codice) {
    				alert ('There was a problem in the AJAX process.');
    			}
    		}
    	}
    
    	// Send the info to the PHP handler page
    	var data = 'estrai.asp?codice=' + codice;
    
    	xmlHttp.open ("GET",data,true);
    	xmlHttp.send (null);
    }
    Ian Anderson
    www.siteguru.co.uk

  19. #19
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, your code is helpful... any value is selected in the:

    Code:
    <select name="codice" onChange="cerca(this.value);">
    I have this output:
    PHP Code:
    There was a problem in the AJAX process
    Why?
    thanks

  20. #20
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Change the alert to this ...
    Code:
    alert ('There was a problem in the AJAX process. Sent was "' + codice + '"; Received was "' + resp + '"');
    What does the alert show now?
    Ian Anderson
    www.siteguru.co.uk

  21. #21
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    Change the alert to this ...
    Code:
    alert ('There was a problem in the AJAX process. Sent was "' + codice + '"; Received was "' + resp + '"');
    What does the alert show now?
    This is the output:
    http://www.filedropper.com/output_4

  22. #22
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sorry but I don't download unsolicited files. Please just state here what the output is.
    Ian Anderson
    www.siteguru.co.uk

  23. #23
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    689
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why is this javascript question coming back to ASP ?

    you want to capture a user selection from a droplist and pass it through a query string to a separate ASP page via ajax.

    The ASP code (in a separate file) should be request.querystring("codice_value") but that is not the major issue.

    with ajax you can't have the ASP code in the ajax sending file.

    you should look into jQuery forms - and send this back to the javascript forum where it belongs.

  24. #24
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    why is this javascript question coming back to ASP ?
    Because the OP started asking ASP questions and posting ASP code in the Javascript forum.

  25. #25
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,156
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Off Topic:

    For a while I was wondering if it was heading towards a move to either the MySQL or Database forum
    Such is web development. It rarely wants to stay neatly compartmentalized into classifications of our making.


Bookmarks

Posting Permissions

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