ajaxcascade.php
$db = mysql_connect("localhost", "alloall", "12345");
mysql_select_db("alloall",$db);
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
if ($_GET['cod_country'] == 1) {
$a = mysql_query("SELECT id, cod_contry FROM alloall_code WHERE cod_zone=1");
// Так как запрос возвращает несколько строк, применяем цикл
print "[";
while($b = mysql_fetch_array($a))
{
print "{value:\"".$b['id']."\",text:\"".$b['cod_contry']."\"}"; //создаем циклический список
}
print "]";
}
else {echo "mysql_error";
exit();
}
}
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/main.css">
<style type="text/css">
<!--
.alloallru {
font-family: Mistral;
font-size: 40x;
font-weight: bold;
}
-->
</style>
<link href="tpl/templates/css/style.css" rel="stylesheet" type="text/css">
<script src="js/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript" src="tpl/templates/js/jquerykeypad.js"></script>
<script type="text/javascript">
(function($){
// очищаем select
$.fn.clearSelect = function() {
return this.each(function(){
if(this.tagName=='SELECT') {
this.options.length = 0;
$(this).attr('disabled','disabled');
}
});
}
// заполняем select
$.fn.fillSelect = function(dataArray) {
return this.clearSelect().each(function(){
if(this.tagName=='SELECT') {
var currentSelect = this;
$.each(dataArray,function(index,data){
var option = new Option(data.text,data.value);
if($.support.cssFloat) {
currentSelect.add(option,null);
} else {
currentSelect.add(option);
}
});
}
});
}
})(jQuery);
</script>
<script type="text/javascript">
$(document).ready(function(){
// выбор автомобиля
function adjustAuto(){
var countryValue = $('#cod_country').val();
var tmpSelect = $('#cod_contry');
if(countryValue.length == 0) {
tmpSelect.attr('disabled','disabled');
tmpSelect.clearSelect();
adjustModel();
} else {
$.getJSON('ajaxcascade.php',{cod_country:countryValue},function(data) { tmpSelect.fillSelect(data).attr('disabled',''); adjustModel(); });
}
};
// выбор модели
function adjustModel(){
var countryValue = $('#cod_country').val();
var autoValue = $('#cod_contry').val();
var tmpSelect = $('#model');
if(countryValue.length == 0||autoValue.length == 0) {
tmpSelect.attr('disabled','disabled');
tmpSelect.clearSelect();
} else {
$.getJSON('cascadeSelectModel.php',{country:countryValue,auto:autoValue},function(data) { tmpSelect.fillSelect(data).attr('disabled',''); });
}
};
$('#cod_country').change(function(){
adjustAuto();
}).change();
$('#cod_contry').change(adjustModel);
$('#model').change(function(){
if($(this).val().length != 0) { alert('Выбор сделан!'); }
});
});
</script>
</head>
<body>
<h3 align="left">Введите номер для поиска</h3>
<form action = "" method="POST" enctype="multipart/form-data">
<table border="0">
<tr>
<td>
<select id="cod_country">
<option value="">Выбрать страну</option>
<option value="1">Германия</option>
<option value="2">Ю.Корея</option>
<option value="3">Япония</option>
</select>
</div>
</td>
<td>
<select id="cod_contry" name="cod_contry" disabled="disabled"></select>
</td>
<td>
<select id="model" name="cod_def" disabled="disabled"></select>
</td>
<td width="100%">
<input type="text" name = "nkod" id="defaultKeypad">
<strong>@allo<span class="alloallru">all</span>.ru</strong>
<input type="submit" value="Поиск" name="submit">
</form>
</td>
</tr>
<tr>
<td><div id="div"></div></td>
</tr>
</table>
</body>
</html>
Покажите мне ошибку в этом коде? Я сижу не понимаю