动态显示选择框中的值在Codeigniter中不起作用

时间:2018-08-15 05:00:30

标签: php mysql codeigniter codeigniter-3 codeigniter-database

我正在尝试在选择框中获取值,因为动态无法正常工作 这是下面的我的控制器代码:

 {% if article.id in favourited_ids %}Yay, you like this article!{% endif %}

以下是我的看法:

public function CreateNewAsset() 
{
    $data['courselist'] = $this->Dashboard_model->getDashboardCourses();
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

    //Validating Name Field
    $this->form_validation->set_rules('topicname', 'Topicname', 'required');

   if ($this->form_validation->run() == FALSE) {
        $this->load->view('template/header');
        $this->load->view('Dashboard/CreateNewAsset');
        $this->load->view('template/footer');
    } else {

        //Setting values for tabel columns           
        $data = array(
            'courseid' => $this->input->post('courseid'),
            'topicname' => $this->input->post('topicname'),
            'refid' => $this->input->post('refid'),
            'startdate' => $this->input->post('startdate'),
            'expectedend' => $this->input->post('expectedend')
        );

        //Transfering data to Model
        $this->Dashboard_model->assetInsert($data);
        $data['message'] = 'Data Inserted Successfully';

        //Loading View
        $this->load->view('template/header');
        $this->load->view('Dashboard/CreateNewAsset', $data);
        $this->load->view('template/footer');
    }
}

我添加了获取值和插入相同的函数。是这个问题吗?我没有在下拉菜单中获取值。有人可以帮我吗? 我收到错误消息,<form action="<?php echo base_url();?>Dashboard/CreateNewAsset" method="post"> <div class="form-row"> <div class="col-md-3">Course:</div> <div class="col-md-9"> <select name="courseid" class="form-control"> <option value="0">Select Course</option> <?php foreach ($courselist as $course) { ?> <option value="<?php echo $course->id; ?>"><?php echo $course->coursename; ?></option> <?php } ?> </select> </div> </div> </form>

2 个答案:

答案 0 :(得分:1)

由于您覆盖$data:

,因此需要更改插入部分
  $insert_data = array(
        'courseid' => $this->input->post('courseid'),
        'topicname' => $this->input->post('topicname'),
        'refid' => $this->input->post('refid'),
        'startdate' => $this->input->post('startdate'),
        'expectedend' => $this->input->post('expectedend')
    );
    $this->Dashboard_model->assetInsert($insert_data);

整个代码应如下所示:

注意:确保您已加载form_validation库和方法getDashboardCourses返回一些数据

public function CreateNewAsset() 
{
   $data['courselist'] = $this->Dashboard_model->getDashboardCourses();
   $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
   $this->form_validation->set_rules('topicname', 'Topicname', 'required');

   if ($this->form_validation->run() == FALSE) {
        $this->load->view('template/header');
        $this->load->view('Dashboard/CreateNewAsset',$data);
        $this->load->view('template/footer');
   } 
   else 
   {
      $insert_data = array(
            'courseid' => $this->input->post('courseid'),
            'topicname' => $this->input->post('topicname'),
            'refid' => $this->input->post('refid'),
            'startdate' => $this->input->post('startdate'),
            'expectedend' => $this->input->post('expectedend')
        );
        $this->Dashboard_model->assetInsert($insert_data);
        $data['message'] = 'Data Inserted Successfully';
        $this->load->view('template/header');
        $this->load->view('Dashboard/CreateNewAsset', $data);
        $this->load->view('template/footer');
    }
}

答案 1 :(得分:0)

您的“视图”找不到courseList变量。

您需要先将courseList添加到data数组中,然后再将其通过表单传递到视图。

例如:-

  

data ['courseList'] = $ this-> getCourseList(); //获取行列表