<?php
//Include the database configuration file
include 'dbconnect.php';

if(!empty($_POST["country_id"])){
    //Fetch all state data
    $query = mysql_query("SELECT * FROM states WHERE country_id = ".$_POST['country_id']." AND status = 1 ORDER BY state_name ASC");
    
    //Count total number of rows
    $rowCount = mysql_num_rows($query);
    
    //State option list
    if($rowCount > 0){
        echo '<option value="">Select state</option>';
        while($row = mysql_fetch_array($query)){ 
            echo '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
        }
    }else{
        echo '<option value="">State not available</option>';
    }
}elseif(!empty($_POST["state_id"])){
    //Fetch all city data
    $query = mysql_query("SELECT * FROM cities WHERE state_id = ".$_POST['state_id']." AND status = 1 ORDER BY city_name ASC");
    
    //Count total number of rows
    $rowCount = mysql_num_rows($query);
    
    //City option list
    if($rowCount > 0){
        echo '<option value="">Select city</option>';
        while($row = mysql_fetch_array($query)){ 
            echo '<option value="'.$row['city_name'].'">'.$row['city_name'].'</option>';
        }
    }else{
        echo '<option value="">City not available</option>';
    }
}
?>