CodeIgniter下拉列表

时间:2018-03-11 18:51:47

标签: codeigniter drop-down-menu

我的表单中有两个下拉列表,在将数据输入到所有字段并单击“提交”按钮后,第二个下拉列表会在向该字段输入数据时出现表单错误。我从数据库中的单独两个表中获取数据。表名是数据库中的“借用者”和“书籍”。

public function form_validation2()
{
    //echo 'OK';
    $this->load->library('form_validation');
    $this->form_validation->set_rules("borrower_name", "Borrower Name", 'required');
    $this->form_validation->set_rules("borrowed_book", "Borrowed Book", 'required');
    $this->form_validation->set_rules("borrowed_date", "Borrowed Date", 'required');
    $this->form_validation->set_rules("lending_date", "Lending Date", 'required');

    if($this->form_validation->run())
    {
        //true
        $this->load->model("main_model");
        $data = array(
            "borrower_name"   =>$this->input->post("borrower_name"),
            "borrowed_book"   =>$this->input->post("borrowed_book"),
            "borrowed_date"   =>$this->input->post("borrowed_date"),
            "lending_date"    =>$this->input->post("lending_date")

        );

        if($this->input->post("insert"))
        {
            $this->main_model->insert_borrowed($data);
            redirect(base_url() . "main/inserted2");
        }
    }
    else
    {
        //false
        $this->borrower_page();
    }
}

 public function borrower_page(){
    // $this->load->view("borrower_page");

    $this->load->helper('url');

    if($this->input->post("btn_for_books_page")){
        redirect(base_url().'main/index');
    }

    $this->load->model("main_model");
    $data["fetch_data2"] = $this->main_model->fetch_data2();

    $data['borrower_name'] = $this->main_model->get_detail_in_dropdown();
    $data['book_name'] = $this->main_model->dropdown2();

    $this->load->view("borrower_page", $data);

}

从视图

 <div class="form-group">
            <label>Borrower Name</label>

            <select class="form-control" name="borrower_name">
                <?php

                foreach($borrower_name as $row)
                {
                    echo '<option value="'.$row->borrower_name.'">'.$row->borrower_name.'</option>';
                }
                ?>
            </select>


            <!--   <input type="text" name="borrower_name" class="form-control">  -->
            <span class="text-danger"><?php echo form_error("borrower_name"); ?></span>
        </div>

        <div class="form-group">
            <label>Borrowed Book</label>

            <select class="form-control" name="book_name">
                <?php

                foreach($book_name as $row1)
                {
                    echo '<option value="'.$row1->book_name.'">'.$row1->book_name.'</option>';
                }
                ?>
            </select>

            <!-- <input type="text" name="borrowed_book" class="form-control"> -->
            <span class="text-danger"><?php echo form_error("borrowed_book"); ?></span>
        </div>

1 个答案:

答案 0 :(得分:4)

您必须将表单输入命名为正确。尝试更改:

<select class="form-control" name="book_name">

要:

<select class="form-control" name="borrowed_book">

在您的视图中更改它,因为您正在提取&#34; borrowed_book&#34;在你的控制器中。