簡體   English   中英

動態顯示選擇框中的值在Codeigniter中不起作用

[英]Displaying values in select box dynamically is not working in codeigniter

我試圖在選擇框中獲取值,因為動態無法正常工作,這是下面的控制器代碼:

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>

我添加了獲取值並插入相同的函數。 這是問題嗎我沒有在下拉列表中獲取值有人可以幫助我這是什么錯誤,並且我得到的錯誤是Undefined variable: courselist

因為您覆蓋$data:所以要這樣更改插入部分$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');
    }
}

您的“視圖”找不到courseList變量。

您需要courseList添加到data數組,然后再將其傳遞給帶有表單的視圖。

例如:-

數據['courseList'] = $ this-> getCourseList(); //獲取行列表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM