<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
//<![CDATA[
var province_id = <?php echo isset($_POST['province_id'])
?intval($_POST['province_id']):'0'; ?>;
var amphur_id = <?php echo isset($_POST['amphur_id'])
?intval($_POST['amphur_id']):'0'; ?>;
var district_id = <?php echo isset($_POST['district_id'])
?intval($_POST['district_id']):'0'; ?>;
var zipcode_id = <?php echo isset($_POST['zipcode_id'])
?intval($_POST['zipcode_id']):'0'; ?>;
function loadSelectBox(id,url,selected){
$.get(
url,{},function(data){
$(id).html(data);
if (selected!=0){
$(id+' option[value='+selected+']').attr('selected','selected');
}
}
);
}
$(function(){
loadSelectBox(
'#province_id',
'geo_combo.php?load=province',
province_id
);
loadSelectBox(
'#amphur_id',
'geo_combo.php?load=amphur&province_id='+province_id,
amphur_id
);
loadSelectBox(
'#district_id',
'geo_combo.php?load=district&hur_id='+amphur_id,
district_id
);
loadSelectBox(
'#zipcode_id',
'geo_combo.php?load=zipcode&district_id='+district_id,
zipcode_id
);
$('#province_id').change(function(e){
var selected = e.target.value;
loadSelectBox(
'#amphur_id',
'geo_combo.php?load=amphur&province_id='+selected,
0
);
$('#district_id :not(option:first)').remove(); //add
});
$('#amphur_id').change(function(e){
var selected = e.target.value;
loadSelectBox(
'#district_id',
'geo_combo.php?load=district&hur_id='+selected,
0
);
$('#zipcode_id :not(option:first)').remove(); //add
});
$('#district_id').change(function(e){
var selected = e.target.value;
loadSelectBox(
'#zipcode_id',
'geo_combo.php?load=zipcode&district_id='+selected,
0
);
$('#zipcode_id').change(function(e){
var selected = e.target.value;
});
});
});
//]]>
</script>
</head>
<body>
<?php
if (isset($_POST['title'])){
include_once 'config.php';
$e = array();
$level = 'district';
// validation
if (empty($_POST['title'])){
$e[] = 'title ไม่ถูกต้อง';
}
if (empty($_POST['zipcode_id'])){
if (empty($_POST['district_id'])){
if (empty($_POST['amphur_id'])){
if (empty($_POST['province_id'])){
$e[] = 'ไม่ได้ระบุจังหวัด';
}
$e[] = 'ไม่ได้ระบุอำเภอ';
$e[] = 'ไม่ได้ระบุตำบล';
$e[] = 'ไม่ได้ระบุรหัสไปรษณีย์';
} else {
$amphur_id = intval($_POST['amphur_id']);
$r = mysql_query("SELECT count(*) FROM `amphur` WHERE `AMPHUR_ID`=$amphur_id");
if (mysql_result($r,0)==0){
$e[] = 'อำเภอไม่ถูกต้อง';
} else {
$r = mysql_query("SELECT count(*) FROM `district` WHERE `AMPHUR_ID`=$amphur_id");
if (mysql_result($r,0)>0){
$e[] = 'ยังไม่ได้ระบุตำบล';
} else {
$level = 'amphur';
//ไม่จำเป็นต้องระบตำบลเพราะอำเภอไม่มีตำบล
}
}
}
}
if (count($e)>0){
echo '<div id="error">',implode('',$e),'</div>';
}
else {
$district_id = intval($_POST['district_id']);
$r = mysql_query("SELECT count(*) FROM `district` WHERE `DISTRICT_ID`=$district_id");
if (mysql_result($r,0)==0){
$e[] = 'ตำบลไม่ถูกต้อง';
}
}
if (count($e)>0){
echo '<div id="error">',implode('',$e),'</div>';
} else {
echo "<div id=\"notice\">valid at level : $level</div>";
$zipcode_id = intval($_POST['$zipcode_id']);
$r = mysql_query("SELECT count(*) FROM `zipcode` WHERE `ZIPCODE_ID`=$zipcode_id");
if (mysql_result($r,0)==0){
$e[] = 'รหัสไปรษณีย์ไม่ถูกต้อง';
}
}
}
elseif ($level == 'district') {
$r = mysql_query("SELECT * FROM `district` WHERE `DISTRICT_ID`=$district_id");
$district_value = $district_id;
$amphur_value = mysql_result($r,0,'AMPHUR_ID');
$province_value = mysql_result($r,0,'PROVINCE_ID');
}
else {
$r = mysql_query("SELECT * FROM `amphur` WHERE `AMPHUR_ID`=$amphur_id");
$amphur_value = mysql_result($r,0,'AMPHUR_ID');
$province_value = mysql_result($r,0,'PROVINCE_ID');
}
}
?>