Skip To Main Content | Home | Sitemap | Contact
Latest Visitors: United States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagKorea  Republic of's flagUnited States's flagRussian Federation's flagChina's flagSweden's flagNetherlands's flagUnited States's flagUnited States's flagChina's flagChina's flagPakistan's flagUnited States's flagUnited States's flagUnited States's flagSpain's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flag
 

How can you update a companion drop down list when the first list changes, by querying the database but not refreshing the whole page?

In compiled VB or VB.NET, ASP and JavaScript you can use the XMLHTTP object.
 Second (remote query) Field: X


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<title>update second drop down from database without posting page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="keywords" content="&quot;read file&quot;,&quot;javascript&quot;,&quot;client side&quot;,&quot;i/o&quot;">
<meta name="description" content="It used to be impossible to do any file input-output with JavaScript. That is no longer true in modern browser. In this example Rod Divilbiss shows how to retrieve the value of a drop down list and update a related drop down list from the server without refreshing the whole page.">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005-2010 Roderick Divilbiss">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="/include/css/all.css">
<link rel="stylesheet" type="text/css" href="/include/css/ee.css">
<script type="text/javascript" src="/include/allPages.js"></script>
<script type="text/javascript">
var sending=false;

//create the Cross-browser XMLHttpRequest object
function getFile(pURL) {
    if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc 
        xmlhttp=new XMLHttpRequest();
        if (xmlhttp.overrideMimeType) {
            xmlhttp.overrideMimeType('text/xml');
        } 
        xmlhttp.onreadystatechange=postFileReady;
        xmlhttp.open("GET", pURL, true);
        xmlhttp.send(null);
    } else if (window.ActiveXObject) { //IE 
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
        if (xmlhttp) {
            xmlhttp.onreadystatechange=postFileReady;
            xmlhttp.open('GET', pURL, false);
            xmlhttp.send();
        }
    }
}

// function to handle asynchronus call
function postFileReady() {
    if (xmlhttp.readyState==4) { 
        if (xmlhttp.status==200) { 
            document.getElementById('nextDropDown').innerHTML=xmlhttp.responseText;
        }
    }
}
</script>
</head>

<body>
<label for="letter">First Field: </label><select size="1" id="letter" name="letter" onclick="getFile('Update-A-Companion-Drop-Down-List-When-The-First-List-Changes-List.asp?l='+this[this.selectedIndex].value)">
  <option value="A">States beginning with A</option>
  <option value="C">States beginning with C</option>
  <option value="D">States beginning with D</option>
  <option value="F">States beginning with F</option>
  <option value="G">States beginning with G</option>
  <option value="H">States beginning with H</option>
  <option value="I">States beginning with I</option>
  <option value="K">States beginning with K</option>
  <option value="L">States beginning with L</option>
  <option value="M">States beginning with M</option>
  <option value="N">States beginning with N</option>
  <option value="O">States beginning with O</option>
  <option value="P">States beginning with P</option>
  <option value="R">States beginning with R</option>
  <option value="S">States beginning with S</option>
  <option value="T">States beginning with T</option>
  <option value="U">States beginning with U</option>
  <option value="V">States beginning with V</option>
  <option value="W">States beginning with W</option>
</select>&nbsp;Second (remote query) Field: <span id="nextDropDown">&nbsp;</span>
</div>

</body>

</html>


This Weeks Most Popular Pages Newest Pages