【发布时间】:2015-10-13 16:03:57
【问题描述】:
我正在尝试将 CodeIgniter 的表单验证功能应用于我的一个视图页面中的输入表单。在该表单中,所有字段都是必需的。如果所有字段都成功填写,然后如果用户单击提交按钮,它将重定向到具有条目列表的另一个视图页面。如果任何字段留空,将显示验证消息。
这是我在第一个视图页面中的表单 - employee_add.php:
<div id="content" class="box">
<form action="<?php echo base_url();?>index.php/employee_adds/insert_employee_db" method="post">
<?php echo validation_errors(); ?>
<fieldset>
<legend id="add_employee_legend">Add New Employee</legend>
<div>
<label id= "emp_id_add_label">Employee ID:</label>
<input type="text" name="emp_id" id = "employee_id_add" placeholder="Employee ID"/>
</div>
<div>
<label id= "emp_name_add_label">Employee Name:</label>
<input type="text" name="emp_name" id = "employee_name_add" placeholder="Employee Name"/>
</div>
<input type="submit" name="submit" id = "employee_info_submit" value="Send"/>
</fieldset>
</form>
</div>
这是我的控制器 - employee_adds.php:
<?php
if ( ! defined('BASEPATH')){ exit('No direct script access allowed');}
class Employee_adds extends CI_Controller
{
function __construct()
{
parent::__construct();
#$this->load->helper('url');
$this->load->model('employee_model');
}
//Show all Employees or validate employees
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('emp_id', 'Employee ID', 'required');
$this->form_validation->set_rules('emp_name', 'Employee Name', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin_logins/employee_add');
}
else
{
$data['employee_list'] = $this->employee_model->get_all_employees();
$this->load->view('admin_logins/employee_list');
}
}
//Insert an employee
public function insert_employee_db()
{
$edata['emp_id'] = $this->input->post('emp_id');
$edata['emp_name'] = $this->input->post('emp_name');
$res = $this->employee_model->insert_employee($edata);
if($res)
{
header('location:'.base_url()."index.php/employee/".$this->index());
}
}
}
?>
这里是名为 admin_logins.php 的母版页控制器:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_logins extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('admin_logins/index');
}
function employee()
{
$this->load->view('admin_logins/employee_add');
}
}
这是我的模型 - employee_model.php:
<?php
class Employee_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
//To retrieve all employees
public function get_all_employees()
{
$query = $this->db->get('employee');
return $query->result();
}
//To add a new employee to the database
public function insert_employee($data)
{
return $this->db->insert('employee', $data);
}
}
?>
另一个名为 employee_list.php 的视图页面将在其中心 div 中加载成功条目列表:
<div id="content" class="box">
<table id = "employee_list_table" width="600">
<tr>
<th scope="col">Employee Id</th>
<th scope="col">Employee Name</th>
</tr>
<?php foreach ($employee_list as $e_key){ ?>
<tr>
<td><?php echo $e_key->emp_id; ?></td>
<td><?php echo $e_key->emp_name; ?></td>
</tr>
<?php }?>
</table>
</div>
每当我尝试运行employee_add.php 查看页面时,都会收到以下错误:
Fatal error: Call to undefined function validation_errors() in C:\wamp\www\CI\application\views\admin_logins\employee_add.php
据我猜测,可能是以下两个原因之一:
- 我的代码设计和布局不正确(视图、控制器和模型中的编码杂乱无章)
- 我的代码语法不正确(很可能是在使用表单和表单验证时)
或者,也许还有其他原因。我只是想不通为什么,以及我的代码中发生错误的确切位置哪里。
EDIT-1:根据用户Ghost 的解决方案,我已在控制器admin_logins.php 的admin_logins/employee 方法中加载了form_validation 库。致命的 PHP 错误消失了,但验证仍然无法正常工作。在将这些字段保留为空后单击提交按钮时,我会收到以下错误消息,而不是验证消息:
A Database Error Occurred
Error Number: 1146
La table 'ci_test.employee' n'existe pas
INSERT INTO `employee` (`emp_id`, `emp_name`) VALUES ('', '')
Filename: C:\wamp\www\CI\system\database\DB_driver.php
我的代码中似乎还有更多的语法错误。
【问题讨论】:
标签: php html mysql codeigniter