$res = $odbc->query("SELECT prov_id as id, prov_name as name FROM tbl_province ORDER BY prov_name");
foreach($res as $item){
if($editdata['0']['prov_id'] == $item['id']){
$head['prov'] .= "<option value='". $item['id'] ."' selected='selected'>". $item['name'] ."</option>";
} else {
$head['prov'] .= "<option value='". $item['id'] ."'>". $item['name'] ."</option>";
}
}
$head['prov'] = "<select id='head_prov' name='data[head_prov]' onchange=\"load_address('p',this.value,'head_prov','head_amp','head_tum');\"><option value='0'>(โปรดเลือก)</option>" . $head['prov'] . "</select>";
$res = $odbc->query("SELECT amp_id as id, amp_name as name FROM tbl_amphur WHERE amp_id = '". $editdata['0']['prov_id'] ."' ORDER BY amp_name");
foreach($res as $item){
if($editdata['0']['amp_id'] == $item['id']){
$head['amp'] .= "<option value='". $item['id'] ."' selected='selected'>". $item['name'] ."</option>";
} else {
$head['amp'] .= "<option value='". $item['id'] ."'>". $item['name'] ."</option>";
}
}
$head['amp'] = "<select id='head_amp' name='data[head_amp]' onchange=\"load_address('a',this.value,'head_prov','head_amp','head_tum');\"><option value='0'>(โปรดเลือก)</option>" . $head['amp'] . "</select>";
$res = $odbc->query("SELECT tum_id as id, tum_name as name FROM tbl_tumbol WHERE tum_id = '". $editdata['0']['tum_id'] ."%' ORDER BY tum_name");
foreach($res as $item){
if($editdata['0']['tum_id'] == $item['id']){
$head['tum'] .= "<option value='". $item['id'] ."' selected='selected'>". $item['name'] ."</option>";
} else {
$head['tum'] .= "<option value='". $item['id'] ."'>". $item['name'] ."</option>";
}
}
$head['tum'] = "<select id='head_tum' name='data[head_tum]' onchange=\"load_address('t',this.value,'head_prov','head_amp','head_tum');\"><option value='0'>(โปรดเลือก)</option>" . $head['tum'] . "</select>";
echo $head['prov']."<br>".$head['amp']."<br>".$head['tum'];
function.js Code (JavaScript)
function load_address(type,value,p,a,t){
op = window.document.getElementById(p);
oa = window.document.getElementById(a);
ot = window.document.getElementById(t);
switch(type){
case "p":
clear_address(oa);
clear_address(ot);
new microAjax("include/ajax_location_xml.php?action=list_amphur&location_id=" + value,function(res){
create_address(oa,res);
});
break;
case "a":
clear_address(ot);
new microAjax("include/ajax_location_xml.php?action=list_tumbol&location_id=" + value,function(res){
create_address(ot,res);
});
break;
case "t":
break;
}
}
function clear_address(obj){
obj.options.length = 1;
}
function create_address(obj,str){
var txt = "";
var com = str.split(",");
var i = 0;
for(i = 0;i< com.length-1 ;i++){
txt = com[i].split(":");
addOption(obj,txt[1],txt[0]);
}
}
function addOption(selectbox,text,value) {
var optn = document.createElement("option");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
ajax_location_xml.php Code (PHP)
switch($_REQUEST['action']){
case "list_region":
$region_id = $_REQUEST['region_id'];
$data = $odbc->query("SELECT * FROM tbl_division WHERE div_id <> '0' ORDER BY div_name");
foreach($data as $item){
if($region_id == $item['div_id'] && $region_id != 0){
$ret .= "<option selected='selected' value='{$item['div_id']}'>{$item['div_name']}</option>";
} else {
$ret .= "<option value='{$item['div_id']}'>{$item['div_name']}</option>";
}
}
echo "<select id='ajax_region_list' onchange=\"setlocation('region_id',this.value);setlocation('location_id','');ajax_load_proviencelist();\"><option value='0'>(โปรดเลือก)</option>" . $ret . "</select>";
break;
case "list_province":
$region_id = $_REQUEST['region_id'];
$location_id = substr($_REQUEST['location_id'],0,2);
$data = $odbc->query("SELECT * FROM tbl_province WHERE div_id = '{$region_id}' ORDER BY prov_name");
if(count($data) == 0){
$data = $odbc->query("SELECT * FROM tbl_province ORDER BY prov_name");
}
foreach($data as $item){
if($location_id == $item['prov_id']){
$ret .= "<option selected='selected' value='{$item['prov_id']}'>{$item['prov_name']}</option>";
} else {
$ret .= "<option value='{$item['prov_id']}'>{$item['prov_name']}</option>";
}
}
echo "<select id='ajax_prov_list' onchange=\"setlocation('location_id',this.value);ajax_load_amphurlist();\"><option value='0'>(โปรดเลือก)</option>" . $ret . "</select>";
break;
case "list_amphur":
$location_id = substr($_REQUEST['location_id'],0,2);
$data = $odbc->query("SELECT * FROM tbl_amphur WHERE prov_id = '{$location_id}' ORDER BY amp_name");
foreach($data as $item){
$ret .= "{$item['amp_id']}:{$item['amp_name']},";
}
echo $ret;
break;
case "list_tumbol":
$location_id = substr($_REQUEST['location_id'],0,4);
$data = $odbc->query("SELECT * FROM tbl_tumbol WHERE amp_id = '{$location_id}' ORDER BY tum_name");
foreach($data as $item){
$ret .= "{$item['tum_id']}:{$item['tum_name']},";
}
echo $ret;
break;
case "list_tambon_leader":
$location_id = substr($_REQUEST['location_id'],0,4);
$data = $odbc->query("SELECT * FROM tbl_tumbol WHERE amp_id = '{$location_id}' ORDER BY tum_name");
foreach($data as $item){
if(substr($_REQUEST['location_id'],0,6) == $item['tum_id']){
$ret .= "<option selected='selected' value='{$item['tum_id']}'>{$item['tum_name']}</option>";
} else {
$ret .= "<option value='{$item['tum_id']}'>{$item['tum_name']}</option>";
}
}
echo "<select onchange=\"window.document.getElementById('leader_tam').value = this.value\"><option value='0'>(กรุณาเลือก)</option>" . $ret . "</select>";
break;
case "list_village":
$location_id = substr($_REQUEST['location_id'],0,6);
$data = $odbc->query("SELECT * FROM tbl_village WHERE tum_id = '{$location_id}' ORDER BY vill_name");
foreach($data as $item){
if(substr($_REQUEST['location_id'],0,8) == $item['vill_id']){
$ret .= "<option selected='selected' value='{$item['vill_id']}'>{$item['vill_name']} (". substr($item['vill_id'],6,2) .")</option>";
} else {
$ret .= "<option value='{$item['vill_id']}'>{$item['vill_name']} (". substr($item['vill_id'],6,2) .")</option>";
}
}
echo "<select id='ajax_vil_list' onchange=\"setlocation('location_id',this.value);\"><option value='{$location_id}'>(โปรดเลือก)</option>" . $ret . "</select>";
break;
}
$odbc->close();
ปัญหาคือพอคลิกเลือกจังหวัดก็เข้า function load_address() แต่ไม่รู้ทำไมตัว listmenu อำเภอ ถึงไม่ขึ้นครับ พอดีเอาโค๊ตจากที่อื่นมาลองทำดูอ่ะครับ