Пытаюсь организовать выпадающие динамические списки при помощи Ajax.
На сервер положил три файла:
index.php, district_select.php и
district.js.
index.php: [PHP]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript" src="district.js"></script>
Область:
<select name="Area" style="width:130px" onchange="AreaList(this)">
<option value="0" selected="selected">Выбор</option>
<option value="1">Крым</option>
<option value="2">Киевская</option>
<option value="3">Волынская</option>
<option value="4">Одесская</option>
</select>
Районный центр:
<select disabled="disabled" name="District_List" style="width:130px" id="District_List">
<option>Выбор</option>
</select>
</body>
</html>
[/PHP]
district.js:
function District_makeRequest(url) {
http_request = false;
if (window.XMLHttpRequest)
{ // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject)
{ // IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{}
}
}
if (!http_request)
{
alert('Не получается отправить XMLHTTP запрос');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('GET', url, true);
http_request.send(null);
}
function alertContents()
{
if (http_request.readyState == 4)
{
var Districties = eval("("+http_request.responseText+")");
fillDistrict_List(Districties);
}
}
var selectedArea = null;
function AreaList(AreaList)
{
if(AreaList.value === "")
{
clearDistrict_List();
return;
}
if(selectedArea !== null)
{
if(selectedArea == AreaList.value)
{
return;
}
}
selectedArea = AreaList.value;
District_makeRequest("district_select.php?district="+AreaList.value+"&rand="+Math.random());
return true;
}
function fillDistrict_List(elements)
{
clearDistrict_List();
var District_List = document.getElementById("District_List");
District_List.disabled = false;
var i = 1;
for(var key in elements)
{
var District = new Option(elements[key], key, false, false);
District_List.options[i++] = District;
}
return true;
}
function clearDistrict_List()
{
var District_List = document.getElementById("District_List");
while (District_List.length > 0) District_List.options[0] = null;
District_List.options[0] = new Option('', 0, false, false);
District_List.disabled = "disabled";
}
district_select.php: [PHP]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?
if(!array_key_exists('town', $_GET)) die();
mysql_connect("ххх", "ххх", "ххх");
mysql_select_db ("ххх");
$query = sprintf("SELECT * FROM code WHERE area='%s'",
mysql_real_escape_string($_GET['town']));
$result = mysql_query($query);
$output = array();
while($row = mysql_fetch_assoc($result))
{
$output[] = '"'.addslashes($row['id']).'": '.
'"'.addslashes($row['name']).'"';
}
if(sizeof($output) > 0)
{
echo '{'. implode(',', $output) .'}';
}
?>
</body>
</html>
[/PHP]
При выборе из списка области ничего не происходит...
Помогите разобраться, что где не так.