473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

133 100+
Hi,

I have a function here:

Expand|Select|Wrap|Line Numbers
  1.     var AdminResponse = "";
  2.  
  3.     function parseResponse(){
  4.  
  5.         var nText = AdminResponse.getElementsByTagName('optionText');
  6.         var nVal = AdminResponse.getElementsByTagName('optionVal');
  7.         document.forms["area"].elements['town'].options.length = 1;
  8.  
  9.         for (i=0; i<nText.length; i++)
  10.             { 
  11.              var nOption = document.createElement('option'); 
  12.              var isText = document.createTextNode(nText[i].firstChild.data); 
  13.              nOption.setAttribute('value',nVal[i].firstChild.data); 
  14.              nOption.appendChild(isText); 
  15.              document.forms["area"]['town'].appendChild(nOption); 
  16.             }
  17.     }
  18.  
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.

Expand|Select|Wrap|Line Numbers
  1. var AdminResponse = "";
  2.  
  3.     function parseResponse(field){
  4.  
  5.         var nText = AdminResponse.getElementsByTagName('optionText');
  6.         var nVal = AdminResponse.getElementsByTagName('optionVal');
  7.         document.forms["area"].elements['field'].options.length = 1;
  8.  
  9.         for (i=0; i<nText.length; i++)
  10.             { 
  11.              var nOption = document.createElement('option'); 
  12.              var isText = document.createTextNode(nText[i].firstChild.data); 
  13.              nOption.setAttribute('value',nVal[i].firstChild.data); 
  14.              nOption.appendChild(isText); 
  15.              document.forms["area"]['field'].appendChild(nOption); 
  16.             }
  17.     }
  18.  
  19.  
On the drop down itself i have put:

Expand|Select|Wrap|Line Numbers
  1. onchange="update(this.value,'town')"
Any ideas?
Jun 18 '08 #1
14 1565
acoder
16,027 Expert Mod 8TB
Change ['field'] to [field]. You want to use the field variable, not the string "field" which probably doesn't exist.
Jun 18 '08 #2
adamjblakey
133 100+
Thank you for your reply,

I have put this now but still does not work.

Expand|Select|Wrap|Line Numbers
  1.     var AdminResponse = "";
  2.  
  3.     function parseResponse(field){
  4.  
  5.         var nText = AdminResponse.getElementsByTagName('optionText');
  6.         var nVal = AdminResponse.getElementsByTagName('optionVal');
  7.         document.forms["area"].elements[field].options.length = 1;
  8.  
  9.         for (i=0; i<nText.length; i++)
  10.             { 
  11.              var nOption = document.createElement('option'); 
  12.              var isText = document.createTextNode(nText[i].firstChild.data); 
  13.              nOption.setAttribute('value',nVal[i].firstChild.data); 
  14.              nOption.appendChild(isText); 
  15.              document.forms["area"][field].appendChild(nOption); 
  16.             }
  17.     }
I get this error:
document.forms.area.elements[field] has no properties
parseResponse(undefined)ajax.js (line 8)
onreadystatechange()ajax.js (line 30)
[Break on this error] document.forms["area"].elements[field].options.length = 1;

Cheers,
Adam
Jun 18 '08 #3
acoder
16,027 Expert Mod 8TB
Are you passing the field name to parseResponse() properly?
Jun 18 '08 #4
r035198x
13,262 8TB
What does your update function look like?
Jun 18 '08 #5
adamjblakey
133 100+
Here is the full set of code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     var AdminResponse = "";
  3.  
  4.     function parseResponse(field){
  5.  
  6.         var nText = AdminResponse.getElementsByTagName('optionText');
  7.         var nVal = AdminResponse.getElementsByTagName('optionVal');
  8.         document.forms["area"].elements[field].options.length = 1;
  9.  
  10.         for (i=0; i<nText.length; i++)
  11.             { 
  12.              var nOption = document.createElement('option'); 
  13.              var isText = document.createTextNode(nText[i].firstChild.data); 
  14.              nOption.setAttribute('value',nVal[i].firstChild.data); 
  15.              nOption.appendChild(isText); 
  16.              document.forms["area"][field].appendChild(nOption); 
  17.             }
  18.     }
  19.  
  20.     function update(nVal){
  21.  
  22.         var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
  23.         AdminRequest.onreadystatechange = function()
  24.             {
  25.               if (AdminRequest.readyState == 4)
  26.                 {
  27.                    if (AdminRequest.status == 200)
  28.                     {
  29.                        AdminResponse = AdminRequest.responseXML;
  30.                        parseResponse();
  31.                     }
  32.                    else     {
  33.                       alert('Error Update.php File '+ AdminRequest.statusText);
  34.                     }
  35.                 }
  36.             }
  37.         var infoStr = "?choice="+nVal;
  38.         AdminRequest.open("GET", "Update.php"+infoStr, true);
  39.         AdminRequest.send(null); 
  40.     }
  41.  
  42.  
Jun 18 '08 #6
r035198x
13,262 8TB
On line 29 of that update method, you seem not to be passing the field.
Jun 18 '08 #7
adamjblakey
133 100+
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.
Jun 18 '08 #8
acoder
16,027 Expert Mod 8TB
The change you need to make is to add a second parameter to update(), say field, and then pass that to parseResponse().
Jun 18 '08 #9
adamjblakey
133 100+
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.
Jun 18 '08 #10
acoder
16,027 Expert Mod 8TB
See the changes below:
Expand|Select|Wrap|Line Numbers
  1. function update(nVal, field){
  2.  
  3.         var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
  4.         AdminRequest.onreadystatechange = function()
  5.             {
  6.               if (AdminRequest.readyState == 4)
  7.                 {
  8.                    if (AdminRequest.status == 200)
  9.                     {
  10.                        AdminResponse = AdminRequest.responseXML;
  11.                        parseResponse(field);
  12.                     }
  13.                    else     {
  14.                       alert('Error Update.php File '+ AdminRequest.statusText);
  15.                     }
  16.                 }
  17.             }
  18.         var infoStr = "?choice="+nVal;
  19.         AdminRequest.open("GET", "Update.php"+infoStr, true);
  20.         AdminRequest.send(null); 
  21.     }
  22.  
Jun 18 '08 #11
r035198x
13,262 8TB
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
Jun 18 '08 #12
acoder
16,027 Expert Mod 8TB
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.
Jun 18 '08 #13
adamjblakey
133 100+
Thank you so much, works perfect now :)
Jun 18 '08 #14
acoder
16,027 Expert Mod 8TB
Glad it works. I hope you understand why it works too.
Jun 18 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Dan | last post by:
Hello all, I am getting records from a db and displaying the records to the user through a drop down menu in an asp page. Each record has 6 fields and I need to display them all to the user in...
31
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked...
3
by: rsteph | last post by:
I have a javascript drop down menu that I borrowed from a website. It utilizes a little .css to help with formatting. The menu works great, and on all 3 of the browsers I'm concerned about; but I am...
1
by: AndiSmith | last post by:
Hi guys, I wondered if anyone could help me with this problem, or even shed some light on the direction I need to take to resolve it? I'm using .NET 2.0 (C# flavor) to build a large user-based...
0
by: tusaar | last post by:
Hi all I am in big need for a drop down menu created with php, mysql and ajax. Exactly, I need three drop down menu (Category, Subcategory and Item). The data of each drop down will come from...
4
by: Bob | last post by:
Hi all, I'm trying to import data, modify the data then insert it into a new table. The code below works fine for it but it takes a really long time for 15,000 odd records. Is there a way I...
4
by: TycoonUK | last post by:
Hi, As I do not have IE7 on my computer, I was wondering if there is a fault in my CSS Menu when using IE7. Please can someone look at my site - http://www.worldofmonopoly.co.uk and tell me...
1
by: priscbean | last post by:
I'm a total newbie. I really need help with this script. Its a drop down menu formatted with a CSS. It's not a formatting issue but i can't get the JS to run. I think I'm having issues calling it or...
2
by: giandeo | last post by:
Hello all, It's almost a couple of weeks since i am struggling to get this code work. Unfortunately, i am stuck. There seems to be no hope... Please Help....... I am working with an asp page...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.