<?php
include '../../lib/conn.php'; 
$jj = $_POST['jenis'];
if ($jj=="provinsi") {
	$provinsi = $_POST['provinsi'];
 
    echo "<option value=''>Pilih Provinsi </option>";
    $query = mysqli_query($conn, "SELECT * FROM provinsi ORDER BY nama ASC");
    while ($row = mysqli_fetch_assoc($query)) {
        if(isset($provinsi) && $row['id_prov'] == $provinsi)
        {
            echo"<option value='$row[id_prov]' selected>$row[nama]</option>";   
        }
        else
        {
            echo"<option value='$row[id_prov]'>$row[nama]</option>";
        }

    }
 }
 elseif($jj=="kabupaten") {
	$provinsi = $_POST['provinsi'];
 
	echo "<option value=''>Pilih Kabupaten</option>";

	$query = mysqli_query($conn, "SELECT * FROM kabupaten WHERE id_prov='$provinsi' ORDER BY nama ASC");
    while ($row = mysqli_fetch_assoc($query)) {
        echo "<option value='" . $row['id_kab'] . "'>" . $row['nama'] . "</option>";
    }
 }
 elseif ($jj=="kecamatan") {
 	$kabupaten = $_POST['kabupaten'];
 
	echo "<option value=''>Pilih Kecamatan</option>";
	$query = mysqli_query($conn, "SELECT * FROM kecamatan WHERE id_kab='$kabupaten' ORDER BY nama ASC");
    while ($row = mysqli_fetch_assoc($query)) {
        echo "<option value='" . $row['id_kec'] . "'>" . $row['nama'] . "</option>";
    }
 }
 elseif ($jj=="kelurahan") {
 	$kecamatan = $_POST['kecamatan'];
 	echo "<option value=''>Pilih Kelurahan</option>";
 	$query = mysqli_query($conn, "SELECT * FROM kelurahan WHERE id_kec='$kecamatan' ORDER BY nama ASC");
    while ($row = mysqli_fetch_assoc($query)) {
        echo "<option value='" . $row['id_kel'] . "'>" . $row['nama'] . "</option>";
    }
 }

?>