2013-02-04 68 views
-1

我正在codeigniter中的form_dropdown()上工作。更改form_dropdown codeigniter

我有表countrystate(ID,COUNTRYNAME,Statename的)

我填充填充上form_dropdown( 'fd_country')COUNTRYNAME &。

現在我想基於選定的國家名顯示州名。請幫忙。

回答

2

看到像我這樣做是 http://help.webhostel.org/h14682223

控制器代碼

public function index() { 
     $this->load->database(); 
     $this->db->select('countryname'); 
     $this->db->group_by('countryname'); 
     $query = $this->db->get('country'); 

     foreach($query->result() as $item) 
      $countries[$item->countryname] = $item->countryname; 

     $data = array('department' => $countries); 

     $this->load->helper('form'); 
     $this->load->view('welcome_message', $data); 
} 

public function get_subdepartment() { 
    $this->load->database(); 
    $this->load->helper('form'); 
    $states = array(); 
    if($this->input->post('dep_selected')) { 
     $this->db->select('countrystate'); 
     $this->db->where(array('countryname' => $this->input->post('dep_selected'))); 
     $query = $this->db->get('country'); 

     foreach($query->result() as $item) 
      $states[$item->countrystate] = $item->countrystate; 
    } 

    $output = form_dropdown('subdept', $states); 
    echo $output; 
} 

模板部分。

<script type="text/javascript"> 
function get_subdepartment() { 
    var dep_selected = $('select[name=txtDept]').val(); 
    $.ajax({ 
     data: { 
      dep_selected: dep_selected, 
     }, 
     type: 'POST', 
     url: '/welcome/get_subdepartment', 
     success: function(data){ 
      console.log(data); 
      $('.subdepartment').html(data); 
     } 
    }) 
} 
</script> 
<div id="container"> 
<h1>Welcome to CodeIgniter</h1> 
<div id="body"> 
<? echo form_open("addEmployee"); $newEmp = array('name' => 'newEmployee', 'id' => 'newEmployee', 'value' => set_value('ne')); ?> 
    <div for='newEmp'><input type="text" name="txtEmpName" id="txtEmpName" maxlength="25" /></div> 
    <div for='newEmp'> 
     <? echo form_dropdown($name = 'txtDept',$Options = $department, array($this->input->post('txtDept')),'onChange="get_subdepartment()"'); ?> 
    </div> 
    <div for='newEmp' class="subdepartment"></div> 
<? echo form_submit('do_submit', 'Submit'); ?> 
<? echo form_close(); ?> 
</div> 

+0

我使用<? echo form_open(); ?>和上面的代碼相同但沒有結果。還有<? form_submit(參數)?>請指導 –

+0

我已更改模板部分。現在看。如果它沒有工作 - 顯示模板的所有部分 –

+0

沒有數據發佈代碼爲:<? echo form_open(「addEmployee」); $ newEmp = array('name'=>'newEmployee','id'=>'newEmployee','value'=> set_value('ne')); ?>

input->post('txtDept')),'onChange="submit(); return false;"'); ?>
<? echo for_submit(); ?><? echo form_close(); ?> –

相關問題