您好我曾试图查看其他人但是,无法根据他们的代码和解决方案找出我的工作原因。如果我禁用(注释掉)表单验证,我可以获得要显示的帖子数据。但如果留在它中总是出现错误,并且没有显示发布数据。我已经自动加载了我的模型,网址,表单,文本,安全性,form_validation
控制器中的
// Log in user
public function login(){
$data['title'] = 'Sign In';
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
// validate rules for login cedentials
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
// if login credentials are not entered (validation is false) show login view
if(!$this->form_validation->run()){
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
echo "Not valid!";
echo "<br>";
print_r($this->input->post());
echo $this->input->post('username');
echo validation_errors();
echo $this->form_validation->run();
} else {
// Get username
$username = $this->input->post('username');
// Get and encrypt the password
$password = $this->input->post('password');
// Login user with model (database)
$user_id = $this->user_model->login($username, $password);
if($user_id){
// Create session
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
// set session user data
$this->session->set_userdata($user_data);
// Set message
$this->session->set_flashdata('user_loggedin', 'You are now logged in');
redirect('users/index');
} else {
// Set message
$this->session->set_flashdata('login_failed', 'Login is invalid');
redirect('users/login');
}
}
} // end function login
以视图形式
<?php echo form_open('users/login'); ?> <!-- the action is the controller users and the function register -->
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="my-input-control-100p">
<div class="form-group">
<input type="text" name="username" class="form-control" placeholder="Enter Username" required autofocus>
</div>
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="Enter Password" required autofocus>
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
<br>
<div class="center_login">
<small><a href="<?php echo site_url("pages/view/register") ?>">Register here</a> </small>
<br>
<br>
<small><a href="<?php echo site_url("pages/view/forgot") ?>">Forgot Password or User Name</a> </small>
</div>
</div>
</div>
<?php echo form_close(); ?>
答案 0 :(得分:0)
这是我使用它的唯一方法,因为它没有给出布尔值但布尔值的数字表示
if($ this-&gt; form_validation-&gt; run()!= 0){
答案 1 :(得分:0)
我也是第一次遇到这个问题。 解决这个错误有两种方法:
第一种方法:
$attribute['method'] = 'post';
echo form_open('users/login',$attribute);
第二种方法:
form method="post" action="php start echo base_url('users/login') php end"
结论:- 你错过了形式的方法。