<?php
$connection = new mysqli("server100.orangehost.com","guruvu_db_user","Rasm@1562","guruvu_db_");
class Common {
    public function getCountries($connection){
        $query = "SELECT * FROM dist";
        $result = $connection->query($query);
        return $result;
    }

    public function getStatesByCountry($connection,$countryId) {
        $query = "SELECT * FROM mandal WHERE countryId='$countryId'";
        $result = $connection->query($query);
        return $result;
    }

    public function getCityByState($connection,$stateId)
    {
        $query = "SELECT * FROM school WHERE state_id='$stateId'";
        $result = $connection->query($query);
        return $result;
    }
}

if (isset($_POST['getStatesByCountry']) == "getStatesByCountry") {
    $statesData = '';
    $countryId = $_POST['countryId'];
    $common = new Common();
    $allStates = $common->getStatesByCountry($connection,$countryId);
    if ($allStates->num_rows>0){
        while ($states = $allStates->fetch_object()) {
            $stateId = $states->id;
            $stateName = $states->statename;
            $statesData .= '<option value="'.$stateId.'">'.$stateName.'</option>';
        }
    }
    echo "test^".$statesData;
}

if (isset($_POST['getCityByState']) == "getCityByState") {
    $cityData = '';
    $stateId = $_POST['stateId'];
    $common = new Common();
    $allCity = $common->getCityByState($connection,$stateId);
    if ($allCity->num_rows>0){
        while ($city = $allCity->fetch_object()) {
            $cityId = $city->id;
            $cityName = $city->cityName;
            $cityData .= '<option value="'.$cityId.'">'.$cityName.'</option>';
        }
    }
    echo "test^".$cityData;
}
?>

