<?php
    session_start();
    $conn = mysql_connect("localhost", "skynetuser", "SkynetUserBlog2017$");
    mysql_select_db("skynet_blog", $conn);

    if(!empty($_POST['country']) && empty($_POST['state'])){
        $html='';
        $html.="<select name='Country' id='Country' class='form-control select2'>";
        $html.="<option value=''>Select Country</option>";
        $qur=mysql_query("select * from countries");
        while($country=mysql_fetch_array($qur)){
            $html.="<option value='".$country['v_iso_2']."'>".$country['name']."</option>";
        }
        $html.="</select>";
        echo $html;
    }
    if(!empty($_POST['state'])&& empty($_POST['city'])){
        $html='';
        $html.="<select name='state' id='state' class='form-control select2'>";
        $html.="<option value=''>Select State</option>";
        $country=!empty($_POST['country'])?$_POST['country']:'IN';
        $conqur=mysql_query("select * from countries where v_iso_2='".$country."'");;
        $condetail=mysql_fetch_array($conqur);
        $qur=mysql_query("select * from states where i_country_id='".$condetail['id']."'");
        while($state=mysql_fetch_array($qur)){
            $html.="<option value='".$state['v_name']."'>".$state['v_name']."</option>";
        }
        $html.="</select>";
        echo $html;
    }
    if(!empty($_POST['city'])){
        $html='';
        $html.="<select name='city' id='city' class='form-control select2'>";
        $html.="<option value=''>Select City</option>";
        $state=!empty($_POST['state'])?$_POST['state']:"Gujarat";
        $conqur=mysql_query("select * from states where v_name='".$state."'");;
        $condetail=mysql_fetch_array($conqur);

        $qur=mysql_query("select * from cities where i_state_id='".$condetail['i_id']."'");
        while($city=mysql_fetch_array($qur)){
            $html.="<option value='".$city['v_name']."'>".$city['v_name']."</option>";
        }
        $html.="</select>";
        echo $html;
    }
?>