<?php 
include 'config/koneksi.php';
 
$data = $_POST['data'];
$id = $_POST['id'];
 
$n=strlen($id);
$m=($n==2?5:($n==5?8:13));
// $wil=($n==2?'Kota/Kab':($n==5?'Kecamatan':'Desa/Kelurahan'));
?>
<?php 
if($data == "kabupaten"){
	?>
	Kabupaten/Kota
	<select id="form_kab">
		<option value="">Pilih Kabupaten/Kota</option>
		<?php 
		$daerah = "SELECT kode,nama FROM wilayah WHERE LEFT(kode,'$n')='$id' AND CHAR_LENGTH(kode)=$m ORDER BY nama";
        $tampil = mysqli_query($konek, $daerah);
		while($t = mysqli_fetch_array($tampil)){
			?>
			<option value="<?php echo $t['kode']; ?>"><?php echo $t['nama']; ?></option>
			<?php 
		}
		?>
	</select>
 
	<?php 
}else if($data == "kecamatan"){ 
	?>
	<select id="form_kec">
		<option value="">Pilih Kecamatan</option>
		<?php 
		$daerah = "SELECT kode,nama FROM wilayah WHERE LEFT(kode,'$n')='$id' AND CHAR_LENGTH(kode)=$m ORDER BY nama";
        $tampil = mysqli_query($konek, $daerah);
		while($t = mysqli_fetch_array($tampil)){
			?>
			<option value="<?php echo $t['kode']; ?>"><?php echo $t['nama']; ?></option>
			<?php 
		}
		?>
	</select>
 
	<?php 
}else if($data == "desa"){ 
	?>
 
	<select id="form_kel">
		<option value="">Pilih Desa</option>
		<?php 
		$daerah ="SELECT kode,nama FROM wilayah WHERE LEFT(kode,'$n')='$id' AND CHAR_LENGTH(kode)=$m ORDER BY nama";
		$tampil = mysqli_query($konek, $daerah);
		while($t = mysqli_fetch_array($tampil)){
			?>
			<option value="<?php echo $t['kode']; ?>"><?php echo $t['nama']; ?></option>
			<?php 
		}
		?>
	</select>
 
	<?php 
 
}
?>