如何使用 Ajax 在 Cakephp 中根据国家/地区 Select 陈述?

How Can I Select State According To Country In Cakephp Using Ajax?

如何使用 Ajax 在 Cakephp 中根据国家/地区 Select 状态?

我想根据 selected 国家/地区添加州列表。例如,如果印度

是 selected 然后印度国家出现在国家下拉等

不是working.when我要select根据国家/地区。

请帮忙 me.below 是我的编码

在控制器中

<?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 ctp file
<script>
$.noConflict();
</script>
<link href="<?php echo HTTP_ROOT;?>css/jquery.validate.css" rel="stylesheet" type="text/css" />
<script src="<?php echo HTTP_ROOT;?>js/jquery.min.1.8.3.js" type="text/javascript"></script>
<script src="<?php echo HTTP_ROOT;?>js/jquery.validate.js" type="text/javascript"></script>
<script src="<?php echo HTTP_ROOT;?>js/jquery.validation.functions.js" type="text/javascript"></script>
<link href="<?php echo HTTP_ROOT;?>css/admin/AdminLTE.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
 function showFields(id) 
{ 
alert(id);
//alert("hello");
dataString="country_id="+id;
alert(dataString);
  $.ajax({
            url: "venues/state",
            type: "POST",
            data: dataString,//alert(data);
   success: function(data)
    {     
     $('#result').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">
              <div id="result">
                <?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>
            <!--<div id="state"></div>-->
               
          </div>
        </div>
        <!-- /.box-body -->
       
      </div>
    </div>
  </div>
</section>

在您的操作中,仅列表并将 id 发送到视图并生成选项,如 -

查看文件 以执行 state 操作

<option value="">Select One</option>
<?php
foreach($selectbox as $key => $value) {
   echo "<option value='".$key."'>".$value."</option>";
}
?>

生成具有 id -

的 select 字段
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'));  ?>

现在在 Ajax success 部分 -

   $.ajax({
        url: "venues/state",
        type: "POST",
        data: dataString,//alert(data);
        success: function(data)
         {     
            $('#state').html(data);
         },
    });

希望它能奏效。

请在您获取状态列表的控制器或模型中回显您的结果..然后尝试在成功函数中提醒数据。 将您的代码格式化为 select dropdown Means like this

<option value="<?php echo state_id;?>"><?php echo $state_name;?></option>

根据蛋糕进行修改php。