I have a database of country and states. I finally got a ajax select menu to work. When you select country, the states are populated automatically for that country without refreshing the page.
It populates the states in Firefox but not in IE.
Just wondered if someone know if there are any bugs with this and IE?
JS file code below:
function GetXmlHttpObject(handler) {
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject(“Microsoft.XMLHTTP”)
}
return objXMLHttp
}
function stateChanged() {
if (xmlHttp.readyState==4 || xmlHttp.readyState==“complete”)
{
document.getElementById(“txtResult”).innerHTML= xmlHttp.responseText;
}
else {
//alert(xmlHttp.status);
}
}
function stateChanged2() {
if (xmlHttp.readyState==4 || xmlHttp.readyState==“complete”)
{
document.getElementById(“txtResult2”).innerHTML= xmlHttp.responseText;
}
else {
//alert(xmlHttp.status);
}
}
// Will populate data based on input
function htmlData(url, qStr) {
if (url.length==0)
{
document.getElementById(“txtResult”).innerHTML=“”;
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert (“Browser does not support HTTP Request”);
return;
}
url=url+"?"+qStr;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true) ;
xmlHttp.send(null);
}
// Will populate data based on input
function htmlData2(url, qStr) {
if (url.length==0)
{
document.getElementById(“txtResult2”).innerHTML=“”;
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert (“Browser does not support HTTP Request”);
return;
}
url=url+"?"+qStr;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged2;
xmlHttp.open("GET",url,true) ;
xmlHttp.send(null);
}
SELECT MENU BELOW:
COUNTRY MENU
<select name=“country_number” onchange=“htmlData(‘data.php?country_number=’+this.value)” class=menu1 />
//CONTENT HERE
</SELECT>
STATE MENU
<select name=‘state_number’ id=“txtResult”>
</select>
data.php file with states from the database below:
<?php
include('include/config.php');
$state_number = $_GET['state_number'];
$country_number = $_GET['country_number'];
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$QUERY39 = mysql_query("SELECT * FROM states WHERE country_number='$country_number' ORDER BY state_rank");
$ROW39 = mysql_num_rows($QUERY39);
if ($ROW39) {
$K39 = 0;
while ($K39 < $ROW39) {
$state_number = mysql_result($QUERY39,$K39,"state_number");
$state_name = mysql_result($QUERY39,$K39,"state_name");
echo("<OPTION VALUE='$state_number'>($state_number) $state_name</OPTION>");
$K39++;
}
}
?>