0

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



} 
4

1 回答 1

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();
于 2012-08-12T09:15:25.157 回答