Codeigniter 验证不工作,页面重定向到空白页面
Codeigniter Validation is not working , page is redirecting to blank page
我是 codeigniter 的新手。这两个页面的位置都是正确的。我想有一些代码可以为视图页面激活 form_validator。
编辑:我添加了
if ($this->form_validation->run()){
$this->load->view('home');
}else{
$this->load->view('login');
}
控制器中的上述代码仍在将页面重定向到空白页面
<?php
class Welcome extends CI_Controller {
function __construct()
{
parent:: __construct();
$this->load->helper(array('form', 'url', 'html'));
$this->load->library('form_validation');
$this->load->database();
}
public function index()
{
$this->form_validation->set_rules('username','username', 'required|min_length[5]|max_length[15]');
$this->form_validation->set_rules('email','email', 'trim|required|valid_email');
$this->form_validation->run();
$this->load->view('login');
{
}
}
?>
这里是查看页面。我也使用纯 html 表单标签而不是 form_open 方法,但它也没有用,它将我重定向到同一页面并且没有显示任何错误
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<div id="container">
<?php echo validation_errors('form'); ?>
<?php echo form_open('form'); ?>
<?php echo form_error('username'); ?>
<input type='text' name ='username' >
<?php echo form_error('email'); ?>
<input type='email' name='email'>
<input type="submit" value='go'>
</form>
</div>
</body>
</html>
试试这个
在视图中
<?php echo form_open('welcome'); ?>
在控制器中
public function index()
{
$this->form_validation->set_rules('username','username', 'required|min_length[5]|max_length[15]');
$this->form_validation->set_rules('email','email', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE)
{
# Fail
$this->load->view('login');
}
else
{
# Success
$this->load->view('home'); # Load your contrller
}
}
使用这个
if ($this->form_validation->run()){
$this->load->view('home');
}else{
$this->load->view('login');
}
更改索引函数的完整主体,如下所示
if (isset($_POST)) {
$this->form_validation->set_rules('username','username','required|min_length[5]|max_length[15]');
$this->form_validation->set_rules('email','email','trim|required|valid_email');
if($this->form_validation->run()){
$this->load->view('home');
}else{
$this->load->view('login');
}
} else {
$this->load->view('login');
}
我是 codeigniter 的新手。这两个页面的位置都是正确的。我想有一些代码可以为视图页面激活 form_validator。
编辑:我添加了
if ($this->form_validation->run()){
$this->load->view('home');
}else{
$this->load->view('login');
}
控制器中的上述代码仍在将页面重定向到空白页面
<?php
class Welcome extends CI_Controller {
function __construct()
{
parent:: __construct();
$this->load->helper(array('form', 'url', 'html'));
$this->load->library('form_validation');
$this->load->database();
}
public function index()
{
$this->form_validation->set_rules('username','username', 'required|min_length[5]|max_length[15]');
$this->form_validation->set_rules('email','email', 'trim|required|valid_email');
$this->form_validation->run();
$this->load->view('login');
{
}
}
?>
这里是查看页面。我也使用纯 html 表单标签而不是 form_open 方法,但它也没有用,它将我重定向到同一页面并且没有显示任何错误
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<div id="container">
<?php echo validation_errors('form'); ?>
<?php echo form_open('form'); ?>
<?php echo form_error('username'); ?>
<input type='text' name ='username' >
<?php echo form_error('email'); ?>
<input type='email' name='email'>
<input type="submit" value='go'>
</form>
</div>
</body>
</html>
试试这个
在视图中
<?php echo form_open('welcome'); ?>
在控制器中
public function index()
{
$this->form_validation->set_rules('username','username', 'required|min_length[5]|max_length[15]');
$this->form_validation->set_rules('email','email', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE)
{
# Fail
$this->load->view('login');
}
else
{
# Success
$this->load->view('home'); # Load your contrller
}
}
使用这个
if ($this->form_validation->run()){
$this->load->view('home');
}else{
$this->load->view('login');
}
更改索引函数的完整主体,如下所示
if (isset($_POST)) {
$this->form_validation->set_rules('username','username','required|min_length[5]|max_length[15]');
$this->form_validation->set_rules('email','email','trim|required|valid_email');
if($this->form_validation->run()){
$this->load->view('home');
}else{
$this->load->view('login');
}
} else {
$this->load->view('login');
}