Ajax Example With Country ,State and City in Codeigniter

0
1)First Create database and Table





Complete Code of Controller:-

<?php
class AjaxExample2 extends CI_Controller
{
   function __construct()
   {
    parent::__construct();
    $this->load->model('stumodel');
   }
   function index()
   {
      $data['r'] = $this->stumodel->getCountry();
    $this->load->view('countryview',$data);
   }

   function searchdata($id)
   {
     
     
      $data['res']=$this->stumodel->statesearch($id);
     
      $this->load->view('stview',$data);
   }

}








?>

Complete Code of Model
<?php
class Stumodel extends CI_Model
{
    function __construct()
    {
    parent::__construct();
    $this->load->database();
    }

     function getCountry()
     {
        return $this->db->get('country')->result_array();
     }
     function statesearch($id)
     {
        return $this->db->get_where('state',array("cid"=>$id))->result_array();
     }

?>

Complete Code of  Load View:

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
select
{
margin-top: 50px;
width: 300px;
height: 40px;
}


</style>
<script type="text/javascript">
function showstate(a)
{

xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange=function()
  {
  document.getElementById("state").innerHTML=xmlhttp.responseText;
  }
  xmlhttp.open("POST","<?php echo site_url(); ?>/ajaxexample2/searchdata/"+a,true);
  xmlhttp.send();
}

</script>
</head>
<body>

<select onchange="showstate(this.value)">
<option value="">Select Country</option>
<?php


foreach($r as $x)
{
?>
     
       <option value="<?php echo $x['countryid']; ?>"><?php echo $x['countryname']; ?></option>
<?php

}
?>

</select>

<div id="state"></div>
</body>
</html>

Complete Code of Stview.php

<!DOCTYPE html>
<html>
<head>
<title></title>

</head>
<body>

<select >

<?php


foreach($res as $x)
{
?>
     
       <option value="<?php echo $x['stateid']; ?>"><?php echo $x['statename']; ?></option>
<?php

}
?>

</select>


</body>
</html>

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)