473,796 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ajax List, Working but need to tweak

133 New Member
Hi,

I am using the following function to do a country/region ajax drop down list.

Expand|Select|Wrap|Line Numbers
  1. // JavaScript Document
  2.  
  3.     var AdminResponse = "";
  4.  
  5.     function parseResponse(){
  6.  
  7.         var nText = AdminResponse.getElementsByTagName('optionText');
  8.         var nVal = AdminResponse.getElementsByTagName('optionVal');
  9.         document.forms[0]['region'].options.length = 1;
  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[0]['region'].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.  
This will bring a list of regions based on what country is selected, what i need though is when a region is selected another list is generated with towns.

So basically i need the above duplicating but don't know which bits need changing.

Thanks in advanced for the help.

Cheers,
Adam
Mar 20 '08 #1
13 1435
acoder
16,027 Recognized Expert Moderator MVP
You will need to add an onchange to the second select element which will either make another Ajax request to get the cities or you could load the cities when you change the region.
Mar 22 '08 #2
adamjblakey
133 New Member
Hi,

Yes i have done this but i need to duplicate the javascript function above to do this but don't know which bits to change to make it so it is not using the same variables etc.

Cheers,
Adam
Mar 27 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
It's "region" that you want to replace with something more generic.

Use a string as an argument in parseResponse() for the name of the select element.
Mar 27 '08 #4
adamjblakey
133 New Member
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[0]['region'].options.length = 1;
  8.         for (i=0; i<nText.length; i++)
  9.             { 
  10.              var nOption = document.createElement('option'); 
  11.              var isText = document.createTextNode(nText[i].firstChild.data); 
  12.              nOption.setAttribute('value',nVal[i].firstChild.data); 
  13.              nOption.appendChild(isText); 
  14.              document.forms[0]['region'].appendChild(nOption);
  15.             }
  16.     }
  17.  
  18.     function update(nVal){
  19.  
  20.         var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
  21.         AdminRequest.onreadystatechange = function()
  22.             {
  23.               if (AdminRequest.readyState == 4)
  24.                 {
  25.                    if (AdminRequest.status == 200)
  26.                     {
  27.                        AdminResponse = AdminRequest.responseXML;
  28.                        parseResponse();
  29.                     }
  30.                    else     {
  31.                       alert('Error Update.php File '+ AdminRequest.statusText);
  32.                     }
  33.                 }
  34.             }
  35.         var infoStr = "?choice="+nVal;
  36.         AdminRequest.open("GET", "Update.php"+infoStr, true);
  37.         AdminRequest.send(null); 
  38.     }
Apr 5 '08 #5
gits
5,390 Recognized Expert Moderator Expert
and what is your question?

kind regards
Apr 5 '08 #6
adamjblakey
133 New Member
Sorry about that i though i had included my question.

What this function does is when i select a region from a list it dynamically fills the town list with the relevant towns.

My problem is that i have 3 fields for regions and 3 towns so i need to replicate this function 3 times but don't know which bits to change so that it is different enough 3 times over.

Please can someone help me replicate this another 2 times.

Thanks in advanced.
Adam
Apr 6 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
Merged threads since it's effectively the same problem.

You just need to make the functions generic using parameters so that they can refer to any number of select elements.
Apr 6 '08 #8
adamjblakey
133 New Member
Thank you for your reply, my knowledge of JavaScript is very limited, would you be able to give me some pointers on what i need to do please.
Apr 7 '08 #9
acoder
16,027 Recognized Expert Moderator MVP
As an example,
Expand|Select|Wrap|Line Numbers
  1.     function parseResponse(field){
  2.  
  3.         var nText = AdminResponse.getElementsByTagName('optionText');
  4.         var nVal = AdminResponse.getElementsByTagName('optionVal');
  5.         document.forms[0][field].options.length = 1;
  6.         for (i=0; i<nText.length; i++)
  7.             { 
  8.              var nOption = document.createElement('option'); 
  9.              var isText = document.createTextNode(nText[i].firstChild.data); 
  10.              nOption.setAttribute('value',nVal[i].firstChild.data); 
  11.              nOption.appendChild(isText); 
  12.              document.forms[0][field].appendChild(nOption);
  13.             }
  14.     }
  15.  
  16.     function update(nVal, field){
  17.  
  18.         var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
  19.         AdminRequest.onreadystatechange = function()
  20.             {
  21.               if (AdminRequest.readyState == 4)
  22.                 {
  23.                    if (AdminRequest.status == 200)
  24.                     {
  25.                        AdminResponse = AdminRequest.responseXML;
  26.                        parseResponse(field);
  27.                     }
  28.                    else     {
  29.                       alert('Error Update.php File '+ AdminRequest.statusText);
  30.                     }
  31.                 }
  32.             }
  33.         var infoStr = "?choice="+nVal;
  34.         AdminRequest.open("GET", "Update.php"+infoStr, true);
  35.         AdminRequest.send(null); 
  36.     }
See how I've added field to update(). This is the name of the field. In parseResponse() , I've replaced "region" with field, so you just need to pass the name of the element when you call update().
Apr 7 '08 #10

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

Similar topics

7
1856
by: David Laub | last post by:
I have stumbled across various Netscape issues, none of which appear to be solvable by tweaking the clientTarget or targetSchema properties. At this point, I'm not even interested in "solving" these problems - I'm more interested in isolating them, .i.e. finding a "complete" list of issues. Here's my list of serious issues found so far. By serious, I mean functionality that fails, as opposed to much less serious (albeit annoying) display...
8
2086
by: DartmanX | last post by:
Hi, Looking for recommendations for a decent API for AJAX work. I need it to be somewhat documented so I can figure out how to actually use it. My most critical need right now is clean code for a "triple" chained selector (state->county->city). Jason
1
2884
by: jay_dev | last post by:
hi all, im using ajax to replace a dropdown list through a server side script in mozilla it works pretty fine but in IE i get the stupid R0625 error and who knows what it is i tried to tweak the code but nothing, IE crashes and my whole application fails im using IE6 help anyone!!
25
2804
by: meltedown | last post by:
This is supposed ot be an example: http://www.ajaxtutorial.net/index.php/2006/11/30/simple-ajax-using-prototype-part-2/ It says : This example is probably the simplest example you will ever find. We are going to use the prototype feature ‘ajax.Updater’ (see part one for more details on prototype).
1
4034
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest technology is implemented on more sites now than ever. Compatibility is no longer an issue (IE, Mozilla and Opera all support it), and the benefits to using it are amazing. There are too many PHP programmers avoiding any
9
2004
by: schmeckel | last post by:
I am trying to use some very basic AJAX functionality (update panel for partial page update) on my website. When I run my webpage in VS 2005, the partial page update works fine. However, when I upload the page to my website and try running it, the page updates as a full page update rather than a partial page update. I believe the web.config I'm using is correct and I have also put the System.Web.Extensions dll in my BIN folder. Can...
4
2546
by: UKuser | last post by:
Hi, I'm working on the following code, which works fine in Firefox, but not in IE. The problem is its not posting the variable to my page and I'm thinking its something wrong with the getElementByID but the code is as per an example on a tutorial website (http://www.tizag.com/ ajaxTutorial/ajax-javascript.php). The Select element is as follows: <select style="width:240px;font-size:8pt" id='servicet'
2
1336
by: rpollard | last post by:
Hi, I am relatively new to Ajax and am wondering if someone can shed a bit of light on the conceptual part of how Ajax handles its communication. Not knowing a whole lot about Ajax when I started coding I copied some code from the Internet that seem to be the accepted way of creating the object and using it. This is the code I am using on the Javascript side: ------------------------------------------------------------------------ var...
7
6672
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: http://www.asp.net/learn/ajax-videos/video-156.aspx (3rd comment - Joe Stagner)
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.