Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How Can I Select State According To Country In Cakephp Using Ajax?
I want to add state list according to the selected country. For e.g if india
is selected then india States appears in states drop down etc.
Its not working.when i am going to select state according to country.
please help me.below is my coding


in controller
PHP
App::uses('AppController', 'Controller');
class VenuesController extends AppController {
public $uses = array('State','Country','Venue','Facility','User','Image');
public $components = array('Custom');
##################add state##############################
   public function state()
    {
	if($this->RequestHandler->isAjax()) {
	$this->layout = 'ajax';
    $countryid=$this->request->data('country_id');
    $selectstate = $this->State->find('list', array('fields' => array('state_name'), 'conditions' => array('State.country_id' => $countryid)));
    $this->set('selectbox',$selectstate); 
	//pr($selectstate);
	}
    }
	}

in addvenue.ctp

XML
<script src="<?php echo HTTP_ROOT;?>js/jquery.min.1.8.3.js" type="text/javascript"></script>
<script type="text/javascript">

function showFields(id)
{
alert(id);
//alert("hello");
dataString="country_id="+id;
  $.ajax({
            url: "venues/state",
            type: "POST",
            data: dataString,
            //alert(data);
            success: function(data)
             {
              $('#state').html(data);
             },
        });
}
</script>

<section  class="content-header">
<h1> Add Venue </h1>
<ol class="breadcrumb">
  <li><a href="http://localhost/locatevenue/users"><i class="fa fa-dashboard"></i> Home</a></li>
  <li class="active">Add Venue</li>
</ol>
</ section>
<!-- Main content -->
<section  class="content">
  <div class="row">
    <!-- left column -->
    <?php echo $this->Session->flash('success_message') ?>
    <div class="col-md-61">
      <!-- general form elements -->
      <div class="box box-primary">
        <div class="box-header">
          <h3 class="box-title">Add Venue</h3>
        </div>
        <div style="width:49%; float:left;">
          <?php   echo $this->Form->create('Venue' ,array('id' =>'myform' ,'name' => 'myform','enctype'=>'multipart/form-data')); ?>
          <div class="box-body">
            <div class="form-group">
              <?php   echo $this->Form->input('country', array('onChange'=>'showFields(this.value)','class'=>'form-control','id' =>'country','type' => 'select','label'=>true,'label'=>'country:','options' => $country ,'empty' => 'Select A Country','required'=>'false'));  ?>
            </div>
            <div class="form-group">
              <?php
               echo $this->Form->input('state', array('class'=>'form-control','id' =>'state','type' => 'select','label'=>true,'label'=>'state:','options' => $state ,'empty' => 'Select A State','required'=>'false'));  ?>
            </div>
            <!--<div id="state"></div>-->
          </div>
        </div>
        <!-- /.box-body -->
      </div>
    </div>
  </div>
</section>
Posted
Updated 27-Jan-15 19:14pm
v5

In country use onchange function
PHP
echo $this->Form->input('country', array('onChange'=>'showFields(this.value)','class'=>'form-control','id' =>'country','type' => 'select','label'=>true,'label'=>'country:','options' => $country ,'empty' => 'Select A Country','required'=>'false'));


then call to the function showFields
script src="jquery.min.1.8.3.js"
<type="text/javascript"></script>
type="text/javascript"></script>
JavaScript
 function showFields(id) 
{ 
  var strURL = '<?php echo HTTP_ROOT; ??>';
   var image = "<?php echo HTTP_ROOT . 'img/ajax-loader-small.gif'; ??>" 
        $('#loader').html("<img src=""+image+"" />");
  $.post(strURL+"venues/ajaxstate",{"country":id},function(data){  
            if(data) { 
			$('#result').html(data); 
			$("#loader").hide(); 
            }
			else
			{
                alert("Error...");	
            }
        });
}


make a page ajaxstate.ctp .placed it in view of that controller
PHP
if($selectbox)
 {
 echo $this->Form->input('Venue.state', array('class'=>'form-control','id' =>'state','type' => 'select','label'=>true,'label'=>'state:','options' => $selectbox ,'empty' => 'Select A State','required'=>'false'));
 }

in controller
PHP
App::uses('AppController', 'Controller');
class VenuesController extends AppController {
public $uses = array('State','Country','Venue','Facility','User','Image');
  public $components = array('RequestHandler', 'Custom');
 
   public function ajaxstate()
    {
	if($this->RequestHandler->isAjax()) {
	$this->layout = 'ajax';
     $countryid=$this->request->data('country');
    $selectstate = $this->State->find('list', array('fields' => array('state_name'), 'conditions' => array('State.country_id' => $countryid)));
    $this->set('selectbox',$selectstate); 
      
	}
    }


in ctp file in place of state
HTML
<div class="form-group">
            <div class="loader" id="loader"></div>
              <div id="result">
                 
              </div>
            </div>
 
Share this answer
 
v8
Fetch from database php ajax jquery
You can create a mysql database of all the contries and then can use the above tutorial for fetching data in dropdowns.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900