代码点火器:相互依赖的下拉列表.已完成的代码



我有一系列相互依赖的选择列表,它们在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(); ?> 

控制

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');
        }

    }

<?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);
    }

} 

您不会将$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();

相关内容

最新更新