我有登录表单,用户名和密码作为用户输入,当我实现CodeIgniter表单验证器时,它不会通过验证。我已经包含了我的表单和验证控制器。
login.php查看文件摘要。
<div class="tab-content">
<div class="tab-pane fade in active" id="user">
<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<br>
<fieldset>
<div class="form-group">
<div class="right-inner-addon">
<i class="glyphicon glyphicon-user"></i> <input
class="form-control input-lg" type="text"
placeholder="Username" id="username">
</div>
</div>
<div class="form-group">
<div class="right-inner-addon">
<i class="glyphicon glyphicon-lock"></i> <input
class="form-control input-lg" type="password"
placeholder="Password" id="passowrd">
</div>
</div>
</fieldset>
<br>
<div class=" text-center">
<input type="submit" class="btn btn-primary" value="SUBMIT" />
</div>
<?php echo form_close(); ?>
</div>
这是validatelogin.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login');
}
else
{
//Go to private area
redirect('home', 'refresh');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' => $row->username
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
我引用了表单验证器文档,但发现我的代码没有任何问题。
答案 0 :(得分:1)
您的输入字段在两个字段中都缺少name
。在字段中添加name="username"
name="password"
。
<fieldset>
<div class="form-group">
<div class="right-inner-addon">
<i class="glyphicon glyphicon-user"></i>
<input class="form-control input-lg" type="text" placeholder="Username" id="username" name="username">
</div>
</div>
<div class="form-group">
<div class="right-inner-addon">
<i class="glyphicon glyphicon-lock"></i>
<input class="form-control input-lg" type="password" placeholder="Password" id="passowrd" name="passowrd">
</div>
</div>
</fieldset>
答案 1 :(得分:0)
您忘记在用户名和密码输入上输入名称:
<input class="form-control input-lg" type="text" placeholder="Username" id="username" name="username">
<input class="form-control input-lg" type="password" placeholder="Password" id="passowrd" name="password">