【问题标题】:Button in view doesn't call method in controller视图中的按钮不调用控制器中的方法
【发布时间】:2016-01-05 04:00:02
【问题描述】:

我有一个问题:我有一个视图需要在我的控制器 EmployeeController 中调用一个方法才能转到添加员工的页面。但它不起作用.. 也适用于其他视图

这是视图:

<body>
         <form class="form-horizontal" id='employeeform' id='employeeform' action="<?php echo base_url();?>EmployeeController/addEmployee" method="post">
          <div class="container">
          <div class="row">
          <div class="col-lg-12 col-sm-12">
               <table class="table table-striped table-hover">
                    <thead>
                         <tr>
                              <th>#</th>
                              <th>ID</th>
                              <th>Naam werknemer</th>
                          <th>Datum aanwerving</th>
                          <th>Salaris</th>

                     </tr>
                </thead>
                <tbody>
                     <?php for ($i = 0; $i < count($employeelist); ++$i) { ?>
                          <tr>
                               <td><?php echo ($i+1); ?></td>
                               <td><?php echo $employeelist[$i]->employee_no; ?></td>
                               <td><?php echo $employeelist[$i]->employee_name; ?></td>
                               <td><?php echo $employeelist[$i]->hired_date; ?></td>
                               <td><?php echo $employeelist[$i]->salary; ?></td>
                          </tr>
                     <?php } ?>
                </tbody>
           </table>
        <input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Add employee" />
      </div>
      </div>
      </div>
      </form>
     </body>

这是控制器:

EmployeeController 类扩展 CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->library('session');
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->database();
    $this->load->library('form_validation');
    //load the employee model
    $this->load->model('employee_model');
}

//index function
function index() {
    $employeelist = $this->employee_model->get_employee_list();
    $data['employeelist'] = $employeelist;
    $this->load->view('employee/employee_add', $data);
}

function createEmployee() {
    $this->form_validation->set_rules('employee_no', 'Employee No', 'trim|required|numeric');
    $this->form_validation->set_rules('employee_name', 'Employee Name', 'trim|required|xss_clean|callback_alpha_only_space');
    $this->form_validation->set_rules('hired_date', 'Hired Date', 'required');
    $this->form_validation->set_rules('salary', 'Salary', 'required|numeric');

    if ($this->form_validation->run() == FALSE) {
        //fail validation
        $this->load->view('employee/employee_add');
    } else {
        //pass validation
        $data = array(
            'employee_no' => $this->input->post('employeeno'),
            'employee_name' => $this->input->post('employeename'),
            'hired_date' => @date('Y-m-d', @strtotime($this->input->post('hireddate'))),
            'salary' => $this->input->post('salary'),
        );

        //insert the form data into database
        $this->db->insert('tbl_employee', $data);

        //display success message

        $employeeresult = $this->employee_model->get_employee_list();
        $data['employeelist'] = $employeeresult;
        //load the department_view
        $this->load->view('employee/employee_view', $data);
        redirect('employee/employee_view');
    }
}

function addEmployee() {
    $this->load->view('employee/employee_add');
    //set validation rules
}
}
?>

我得到的错误是: 在此服务器上找不到请求的 URL /BoMaTec_afstudeerproject/CodeIgniter-3.0.1/EmployeeController/createEmployee。

【问题讨论】:

  • 如果从视图中的表单操作中删除&lt;?php echo base_url();?&gt; 是否有效?
  • 为什么你的表单标签上有两个id? id='employeeform' id='employeeform' 。另外,当您在开发工具中查看时,html 显示为表单的操作是什么?
  • no.. 我添加了基本网址,因为我在互联网上发现它更好。但是没有,它也不起作用

标签: php html model-view-controller


【解决方案1】:

您似乎没有该网址的任何路由器,或者路由器错误。根据错误,它正在尝试加载一个名为EmployeeController 的文件夹,并在该文件夹中加载另一个名为createEmployee 的文件夹。检查您正在使用的框架中的 .htaccess 文件和路由器。

【讨论】:

  • 那可能是,你知道我需要补充什么吗?
猜你喜欢
  • 2023-03-05
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多