Так я не менял ничего почти в коде, который по ссылке из первого сообщения, разве что базу подключил:
<?php
if (!isset($_REQUEST["type"])) $_REQUEST["type"] = "";
//ajax flag
$ajax_flag = $_REQUEST["type"] == "ajax" ? 1 : 0;
// shopping cart
if ($ajax_flag) include("../cfg/ajax_connect.inc.php"); //section fo ajax
if (isset($_GET['country_id']))
{
$country_id = @intval($_GET['country_id']);
$regs = mysql_query("SELECT name, region_id FROM region WHERE country_id=$country_id");
if ($regs) {
$num = mysql_num_rows($regs);
$i = 0;
while ($i < $num) {
$regions[$i] = mysql_fetch_assoc($regs);
$i++;
}
$result = array('regions'=>$regions);
}
else {
$result = array('type'=>'error');
}
print json_encode($result);
}
?>
скрипт:
$(document).ready(function () {
$('#country_id').change(function () {
var country_id = $(this).val();
if (country_id == '0') {
$('#region_id').html('<option>- выберите регион -</option>');
$('#region_id').attr('disabled', true);
$('#city_id').html('<option>- выберите город -</option>');
$('#city_id').attr('disabled', true);
return(false);
}
$('#region_id').attr('disabled', true);
$('#region_id').html('<option>загрузка...</option>');
//var url = '/index.php';
var url = '/includes/get_regions.php';
$.get(
url,
"country_id=" + country_id + "&type=ajax",
function (result) {
if (result.type == 'error') {
alert('error');
return(false);
}
else {
var options = '';
$(result.regions).each(function() {
options += '<option value="' + $(this).attr('region_id') + '">' + $(this).attr('name') + '</option>';
});
$('#region_id').html('<option value="0">- выберите регион -</option>'+options);
$('#region_id').attr('disabled', false);
$('#city_id').html('<option>- выберите город -</option>');
$('#city_id').attr('disabled', true);
}
},
"json"
);
});
$('#region_id').change(function () {
var region_id = $(this).val(); //$('#region_id :selected').val();
//alert (region_id);
if (region_id == '0') {
$('#city_id').html('<option>- выберите город -</option>');
$('#city_id').attr('disabled', true);
return(false);
}
$('#city_id').attr('disabled', true);
$('#city_id').html('<option>загрузка...</option>');
var url = '/includes/get_city.php';
//var url = 'index.php';
$.get(
url,
"region_id=" + region_id + "&type=ajax",
function (result) {
if (result.type == 'error') {
alert('error');
return(false);
}
else {
var options = '';
$(result.citys).each(function() {
options += '<option value="' + $(this).attr('city_id') + '">' + $(this).attr('name') + '</option>';
});
$('#city_id').html('<option value="0">- выберите город -</option>'+options);
$('#city_id').attr('disabled', false);
$('#city_id').change(function(){
var value = $('#city_id :selected').text();
var city_id = $('#city_id :selected').val();
if (city_id !== '0') {
$('#selectBoxInfo').html('Выбран '+ value).
fadeIn(1000,function(){
$(this).fadeOut(2000);
});
}
});
}
},
"json"
);
});
});