Modify region/town Ajax drop down menu function to work on three different fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    Modify region/town Ajax drop down menu function to work on three different fields

    Hi,

    I have a function here:

    Code:
    	var AdminResponse = "";
    
    	function parseResponse(){
    
    		var nText = AdminResponse.getElementsByTagName('optionText');
    		var nVal = AdminResponse.getElementsByTagName('optionVal');
    		document.forms["area"].elements['town'].options.length = 1;
    		
    		for (i=0; i<nText.length; i++)
    			{ 
    			 var nOption = document.createElement('option'); 
    			 var isText = document.createTextNode(nText[i].firstChild.data); 
    			 nOption.setAttribute('value',nVal[i].firstChild.data); 
    			 nOption.appendChild(isText); 
    			 document.forms["area"]['town'].appendChild(nOption); 
    			}
    	}
    This is part of a Ajax drop down menu for region and town, i want to mod this so that it can work on three different fields as i have three selections for region and town.

    I thought this would work but it doesn't.

    Code:
    var AdminResponse = "";
    
    	function parseResponse(field){
    
    		var nText = AdminResponse.getElementsByTagName('optionText');
    		var nVal = AdminResponse.getElementsByTagName('optionVal');
    		document.forms["area"].elements['field'].options.length = 1;
    		
    		for (i=0; i<nText.length; i++)
    			{ 
    			 var nOption = document.createElement('option'); 
    			 var isText = document.createTextNode(nText[i].firstChild.data); 
    			 nOption.setAttribute('value',nVal[i].firstChild.data); 
    			 nOption.appendChild(isText); 
    			 document.forms["area"]['field'].appendChild(nOption); 
    			}
    	}
    On the drop down itself i have put:

    Code:
    onchange="update(this.value,'town')"
    Any ideas?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Change ['field'] to [field]. You want to use the field variable, not the string "field" which probably doesn't exist.

    Comment

    • adamjblakey
      New Member
      • Jan 2008
      • 133

      #3
      Thank you for your reply,

      I have put this now but still does not work.

      Code:
      	var AdminResponse = "";
      
      	function parseResponse(field){
      
      		var nText = AdminResponse.getElementsByTagName('optionText');
      		var nVal = AdminResponse.getElementsByTagName('optionVal');
      		document.forms["area"].elements[field].options.length = 1;
      		
      		for (i=0; i<nText.length; i++)
      			{ 
      			 var nOption = document.createElement('option'); 
      			 var isText = document.createTextNode(nText[i].firstChild.data); 
      			 nOption.setAttribute('value',nVal[i].firstChild.data); 
      			 nOption.appendChild(isText); 
      			 document.forms["area"][field].appendChild(nOption); 
      			}
      	}
      I get this error:
      document.forms. area.elements[field] has no properties
      parseResponse(u ndefined)ajax.j s (line 8)
      onreadystatecha nge()ajax.js (line 30)
      [Break on this error] document.forms["area"].elements[field].options.length = 1;

      Cheers,
      Adam

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Are you passing the field name to parseResponse() properly?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          What does your update function look like?

          Comment

          • adamjblakey
            New Member
            • Jan 2008
            • 133

            #6
            Here is the full set of code:

            Code:
            	var AdminResponse = "";
            
            	function parseResponse(field){
            
            		var nText = AdminResponse.getElementsByTagName('optionText');
            		var nVal = AdminResponse.getElementsByTagName('optionVal');
            		document.forms["area"].elements[field].options.length = 1;
            		
            		for (i=0; i<nText.length; i++)
            			{ 
            			 var nOption = document.createElement('option'); 
            			 var isText = document.createTextNode(nText[i].firstChild.data); 
            			 nOption.setAttribute('value',nVal[i].firstChild.data); 
            			 nOption.appendChild(isText); 
            			 document.forms["area"][field].appendChild(nOption); 
            			}
            	}
            
            	function update(nVal){
            
            		var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
            		AdminRequest.onreadystatechange = function()
            			{
            		 	 if (AdminRequest.readyState == 4)
            				{
            		 	 	 if (AdminRequest.status == 200)
            					{
            			 	 	 AdminResponse = AdminRequest.responseXML;
            			 	 	 parseResponse();
            					}
            		 	 	 else 	{
            				 	 alert('Error Update.php File '+ AdminRequest.statusText);
            					}
            				}
            			}
            		var infoStr = "?choice="+nVal;
            		AdminRequest.open("GET", "Update.php"+infoStr, true);
            		AdminRequest.send(null); 
            	}

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              On line 29 of that update method, you seem not to be passing the field.

              Comment

              • adamjblakey
                New Member
                • Jan 2008
                • 133

                #8
                If this was the case would it not work on one field? As it works fine when i specify the field just not when i want it to work on multiple.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  The change you need to make is to add a second parameter to update(), say field, and then pass that to parseResponse() .

                  Comment

                  • adamjblakey
                    New Member
                    • Jan 2008
                    • 133

                    #10
                    I am not entirely sure what i need to do here as i just download this from a site and did not build it. I don't really know javascript to well.

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      See the changes below:
                      Code:
                      function update(nVal, field){
                      
                      		var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
                      		AdminRequest.onreadystatechange = function()
                      			{
                      		 	 if (AdminRequest.readyState == 4)
                      				{
                      		 	 	 if (AdminRequest.status == 200)
                      					{
                      			 	 	 AdminResponse = AdminRequest.responseXML;
                      			 	 	 parseResponse(field);
                      					}
                      		 	 	 else 	{
                      				 	 alert('Error Update.php File '+ AdminRequest.statusText);
                      					}
                      				}
                      			}
                      		var infoStr = "?choice="+nVal;
                      		AdminRequest.open("GET", "Update.php"+infoStr, true);
                      		AdminRequest.send(null); 
                      	}

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by adamjblakey
                        I am not entirely sure what i need to do here as i just download this from a site and did not build it. I don't really know javascript to well.
                        Trace your code from the moment the JS function update is triggered. What you need to be checking for is whether or not all the objects being accessed have been passed properly to all the functions that are called after that. Don't be shy to refer to a Javascript functions tutorial while you are at it.

                        Edit: Too late again

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by r035198x
                          Trace your code from the moment the JS function update is triggered. What you need to be checking for is whether or not all the objects being accessed have been passed properly to all the functions that are called after that. Don't be shy to refer to a Javascript functions tutorial while you are at it.
                          This is more useful than what I've posted if "teaching a man to fish" is the objective.

                          Comment

                          • adamjblakey
                            New Member
                            • Jan 2008
                            • 133

                            #14
                            Thank you so much, works perfect now :)

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Glad it works. I hope you understand why it works too.

                              Comment

                              Working...