我有一系列相互依赖的选择列表,可以在CI之外正常工作。当我尝试在Codeigniter中实现它时,我没有进入,因为第一个选择列表没有填充,回显不是正确的代码或我不知道是什么。这里的问题只涉及第一个选择列表,也就是说,你不会看到任何jquery,因为第一个直接从数据库中填充而没有任何“功能改变”。
以下是模块:
查看
<?php echo form_open('control_form/add_all'); ?>
<label for="f_state">State<span class="red">*</span></label>
<select id="f_state" name="f_state">
<option value=""></option>
<?php
foreach($result as $row)
{
echo '<option value="' . $row->pais_id . '">' . $row->pais_name . '</option>';
}
?>
</select>
<label for="f_city">City<span class="red">*</span></label>
<!--this will be filled based on the tree selection above-->
<select id="f_city" name="f_city" id="f_city_label">
<option value=""></option>
</select>
<label for="f_membername">Member Name<span class="red">*</span></label>
<input type="text" name="f_membername"/>
<?php echo form_close(); ?>
CONTROL
public function add_all()
{
#Validate entry form information
$this->load->model('model_form','', TRUE);
$this->form_validation->set_rules('f_state', 'State', 'required');
$this->form_validation->set_rules('f_city', 'City', 'required');
$this->form_validation->set_rules('f_membername', 'Member Name', 'required');
$data['city'] = $this->model_form->get_state(); //gets the available groups for the dropdown
if ($this->form_validation->run() == FALSE)
{
$this->load->view('view_form_all', $data); # parece ser que vuelve a meter los mismos datos que tenia la Form
}
else
{
#Add Member to Database
$this->model_form->add_all();
$this->load->view('view_form_success');
}
}
MODEL
<?php
class Model_form extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_state()
{
$query = $this->db->query('SELECT pais_id, pais_name FROM pais');
return $query->result();
}
function add_all()
{
$v_state = $this->input->post('f_state');
$v_membername = $this->input->post('f_membername');
$data = array(
'pais_id' => NULL,
'pais_name' => $v_state
);
$this->db->insert('members', $data);
}
}
答案 0 :(得分:0)
您没有向视图发送$result
,您需要以下内容:
$data['result'] = ...//get some data
$this->load->view('view_form_all',$data);
如果您要显示的列表是城市列表,则需要更改视图:
foreach($city as $row)
{
echo '<option value="' . $row->pais_id . '">' . $row->pais_name . '</option>';
}
因为在你的控制器中你正在做:
$data['city'] = $this->model_form->get_state();