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

flvtvl50  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(554)

我正在尝试在选择框中获取值,因为dynamic不起作用。下面是我的控制器代码:

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>

我在同一个函数中添加了get值和insert。这是不是我在下拉列表中没有得到值的问题?有人能帮我吗?什么是错误,我得到的是错误 Undefined variable: courselist

iqxoj9l9

iqxoj9l91#

你的“视图”找不到 courseList 变量。
你需要做加法 courseList 给你的 data 数组,然后将其与窗体一起传递给视图。
例如:
data['courselist']=$this->getcourselist();//获取行列表

thtygnil

thtygnil2#

像这样更改插入零件,因为u替代 $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('', '');
$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');
}
}

相关问题