2 combo box and an update

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
eyequbeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
cameroncookeConnect With a Mentor Commented:
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
 
sistemuCommented:
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
 
cameroncookeCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
eyequbeAuthor Commented:
Once i get a complete answer i will happily close this question
0
 
Ray PaseurCommented:
@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
 
Michel PlungjanIT ExpertCommented:
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
All Courses

From novice to tech pro — start learning today.