Solved

2 combo box and an update

Posted on 2008-10-02
8
853 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
8 Comments
 
LVL 3

Accepted Solution

by:
cameroncooke earned 500 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now