php - Codeigniter ajax 国家/地区下拉列表不起作用

标签 php jquery mysql ajax codeigniter

您好,我有注册表单,其中包含国家和下拉列表。在选择国家后无法获得州列表。在控制台中收到响应,因为找不到页面。这是我的代码。

Controller :

function signup()
{
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');

    $this->form_validation->set_rules('country_id','Country Name','required');
    $this->form_validation->set_rules('state_id','State Name','required');
    $this->form_validation->set_rules('city_id','City Name','required');
    if($this->form_validation->run()== FALSE)   
   {    
    $data['country'] = $this->signup_model->getcountry();  
    $data['mainpage']='signup';
    $this->load->view('templates/template',$data);
    }
else
    {
        $this -> signup_model -> insertprofiledetails();
        $this->flash->success('<h2>Profile Details Added Successfully!</h2>');
        redirect('welcome/add');
    }
}

public function ajax_state_list($country_id)
{
    $this->load->helper('url');
    $this->load->model('signup_model');
    $data['state'] = $this->signup_model->getstate($country_id);
    $this->load->view('ajax_get_state',$data);
}

型号:

function getcountry()
 {
    $this -> db -> select('*');
    $query = $this -> db -> get('countries');       
    return $query->result();
 }

  function getstate($country_id='')
 {
    $this -> db -> select('states.*');
    $this -> db -> where('country_id', $country_id);
    $query = $this -> db -> get('states');
    return $query->result();
 }

查看:

<script>
    function getstatedetails(id)
    {
        //alert('this id value :'+id);
        $.ajax({
            type: "POST",
            url: '<?php echo base_url();?>welcome/ajax_state_list '+id,
            data: id='cat_id',
            success: function(data){
                //alert(data);
                $('#old_state').html(data);
        },
         });
    }
</script>
<div class="form-group">
    <label class="sr-only" for="form-address2">Country</label>                                              
    <select name="country_details" class="form-control countries" id="country_details" onChange="getstatedetails(this.value)">
          <option value="" selected="selected" >Select Country</option>
           <?php foreach($country as $count): ?>
            <option value="<?php echo $count->country_id; ?>"><?php echo $count->country_name; ?></option>
            <?php endforeach; ?> 
    </select>
</div>
<div class="form-group">
    <label class="sr-only" for="form-address2">State</label>
    <select name="select_state" class="form-control countries" id="old_state" onChange="getcitydetails(this.value)">
           <option selected="selected">Select State</option>
    </select>
</div>

ajax_get_state:

<select name="select" onChange="getstatedetails(this.value)">
 <option value="" selected="selected" >Select State</option>
 <?php foreach($state as $stt): ?>
  <option value="<?php echo $stt->state_id; ?>"><?php echo $stt->state_name; ?></option>
 <?php endforeach; ?> 
 </select> 

最佳答案

我认为你忘记在 ajax_state_list 之后使用 /

编辑你的 jQuery 脚本:

url: '<?php echo base_url();?>welcome/ajax_state_list/'+id,

关于php - Codeigniter ajax 国家/地区下拉列表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40281288/

相关文章:

jquery - 在本地存储保存功能上,也将变量传递给 MySQL 数据库

javascript - 如何在 jQuery 中重新计算单击时的变量?

javascript - 如何获得 prev() 而不仅仅是 sibling

php - 动态更改默认连接

java - Spring JDBC 给出错误 :"queryForObject(String, Object[], Class<String>) from the type JdbcTemplate refers to the missing type DataAccessException "

php - WordPress:如何按meta_value按降序顺序从wp_posts和wp_postmeta获取记录?

php - 使用同一查询更新两行

java - 使用 Flyway 的 MySQL JDBC 驱动程序

php - Go 和 PHP 中的 SHA256 给出不同的结果

php - 如何使用Tmail发送邮件?