Php 关于更改表单_下拉式代码点火器
我正在编写codeigniter中的表单_下拉列表() 我有表countrystate(id、countryname、statename) 我填写了countryname并填写在表格下拉列表(“fd\U国家”) 现在我想基于所选的countryname显示statename。请帮帮我。就像我做的那样 控制器代码Php 关于更改表单_下拉式代码点火器,php,codeigniter,Php,Codeigniter,我正在编写codeigniter中的表单_下拉列表() 我有表countrystate(id、countryname、statename) 我填写了countryname并填写在表格下拉列表(“fd\U国家”) 现在我想基于所选的countryname显示statename。请帮帮我。就像我做的那样 控制器代码 public function index() { $this->load->database(); $this->db->se
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>
功能get_子部门(){
var dep_selected=$('select[name=txtDept]')。val();
$.ajax({
数据:{
已选择dep_:已选择dep_,
},
键入:“POST”,
url:“/welcome/get_subdepartment”,
成功:功能(数据){
控制台日志(数据);
$('.subdepartment').html(数据);
}
})
}
欢迎来到CodeIgniter
我正在使用&编写与上述代码相同的代码,但没有结果。还有,我已经改变了模板部分。现在看。如果它不起作用——将templateNo数据发布代码的所有部分显示为:那么您需要使用ajax。你看,它现在起作用了。代码已更改。不要忘记要求jqueryIt不是通过javascript发布的,即$this->input->post('txtDept')为null或空白。接下来呢?