Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

2 combo box and an update

Posted on 2008-10-02
8
Medium Priority
?
862 Views
Last Modified: 2013-12-12
Hi,

        I have two combo boxes on a page reading data from a sql table. When Box A changes its select item , combo box B should reflect relevant field. For example, if Box A is showing all the countries of the world. Box B should reflect the relevant cities. If A has india, B should show delhi, mumbai, bangalore.....etc.
How is it possible to do this.

BRgds,

kNish
0
Comment
Question by:eyeqube
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Accepted Solution

by:
cameroncooke earned 1500 total points
ID: 22631816
Someone else asked a similar question for which I answered with this solution. This one us Operating Systems and Versions. Thanks.

Just change the options in the first select to countries, and then the switch statement arrays to the cities.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <title></title>
 <script type="text/JavaScript">
 
   function init() {
     addEvent(document.getElementById('osChoices'), 'change', handleOSChange);
   }
 
   function handleOSChange(e) {
     var elm    = e.target || e.srcElement;
     var verSel = document.getElementById('osVersions');
     var aryVersions = new Array();
 
     verSel.options.length = 0;
 
     switch(elm.options[elm.selectedIndex].value) {
 
       case 'XP':
         aryVersions = new Array('Home', 'Pro', 'Server');
         break;
 
       case 'Vista':
         aryVersions = new Array('Vista Home', 'Vista Pro', 'Vista Ultimate');
         break;
     }
 
     for(var i = 0, k = aryVersions.length; i < k; i++) {
 
       var newOption = document.createElement('OPTION');
       newOption.value = aryVersions[i];
       newOption.innerHTML = aryVersions[i];
       verSel.appendChild(newOption);
     }
   }
 
   function addEvent( obj, type, fn ) {
     if ( obj.attachEvent ) {
       obj['e'+type+fn] = fn;
       obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
       obj.attachEvent( 'on'+type, obj[type+fn] );
     } else
       obj.addEventListener( type, fn, false );
   }
 
 </script>
</head>
 
<body onload="init();">
 
  <select id="osChoices">
    <option value="">Select OS</option>
    <option value="XP">Windows XP</option>
    <option value="Vista">Windows Vista</option>
  </select>
 
  <br /><br />
 
  <select id="osVersions">
    <option value=""><option>
  </select>
 
</body>
</html>

Open in new window

0
 
LVL 3

Expert Comment

by:sistemu
ID: 22632379
Hi,
This is a problem usually solved with AJAX... the sublist would be huge if you would build it for the entire world, wasting the bandwidth loading the page for the first time.
Instead you should write a callback after selecting the country and ask from the server the list of available cities.
0
 
LVL 3

Expert Comment

by:cameroncooke
ID: 22633439
sistemu, I agre that using AJAX would make more sense, but the only asked how to change one select based on the value of the other, for which I have provided a solution.

0
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 

Author Comment

by:eyeqube
ID: 23876695
Once i get a complete answer i will happily close this question
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23884269
@eyeqube, you can choose to model your programs on the answer from cameroncooke, or you can go with AJAX.  Here is how I would choose if it were up to me.

Let's say you want to list all 234 country codes, and the fifty largest cities in each of the countries.  That would mean you would send tens of thousands of lines of code to the browser if you went with the prepopulated select fields.  Maybe that is too much data.  In that case, I might choose AJAX, especially if the clients are using slow connections.

Let's say you want to list only a dozen country codes and the four largest cities each.  That is MUCH less data and the overhead of the AJAX call might not be worth the trouble.

AJAX is a lot simpler than some people make out.  I cannot recall where I got this originally, but it is a few years old and signed "Rasmus" so it might be from Lerdorf, of PHP fame.  It makes it very clear how AJAX works.  Everything else is built on top of this paradigm.  

HTH, ~Ray
I find a lot of this AJAX stuff a bit of a hype.  Lots of people have
been using similar things long before it became "AJAX".  And it really
isn't as complicated as a lot of people make it out to be.  Here is a
simple example from one of my apps.  
 
First the Javascript:
 
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
 
var http = createRequestObject();
 
function sndReq(action) {
    http.open('get', 'rpc.php?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}
 
function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();
 
        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            document.getElementById(update[0]).innerHTML = update[1];
        }
    }
}
 
This creates a request object along with a send request and handle
response function.  So to actually use it, you could include this js in
your page.  Then to make one of these backend requests you would tie it
to something.  Like an onclick event or a straight href like this:
 
  <a href="javascript:sndReq('foo')">[foo]</a>
 
That means that when someone clicks on that link what actually happens
is that a backend request to rpc.php?action=foo will be sent.
 
In rpc.php you might have something like this:
 
  switch($_REQUEST['action']) {
    case 'foo':
      / do something /
      echo "foo|foo done";
      break;
    ...
  }
 
Now, look at handleResponse.  It parses the "foo|foo done" string and
splits it on the '|' and uses whatever is before the '|' as the dom
element id in your page and the part after as the new innerHTML of that
element.  That means if you have a div tag like this in your page:
 
  <div id="foo">
  </div>
 
Once you click on that link, that will dynamically be changed to:
 
  <div id="foo">
  foo done
  </div>
 
That's all there is to it.  Everything else is just building on top of
this.  Replacing my simple response "id|text" syntax with a richer XML
format and makine the request much more complicated as well.  Before you
blindly install large "AJAX" libraries, have a go at rolling your own
functionality so you know exactly how it works and you only make it as
complicated as you need.  Often you don't need much more than what I
have shown here.
 
Expanding this approach a bit to send multiple parameters in the
request, for example, would be really simple.  Something like:
 
  function sndReqArg(action,arg) {
    http.open('get', 'rpc.php?action='+action+'&arg='+arg);
    http.onreadystatechange = handleResponse;
    http.send(null);
  }
 
And your handleResponse can easily be expanded to do much more
interesting things than just replacing the contents of a div.
 
-Rasmus

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 23885739
You DO want use onClick and return false instead of

<a href="javascript:sndReq('foo')">[foo]</a>

use

<a href="#" onClick="sndReq('foo'); return false">[foo]</a>
 
0

Featured Post

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question