[英]Codeigniter can't login
Here is the controller, when I click the login button, nothing happens. 这是控制器,当我单击登录按钮时,什么也没有发生。 What I want is to load the success screen when user data is validated and show error messages when user data is not validated.
我想要的是在验证用户数据时加载成功屏幕,并在未验证用户数据时显示错误消息。
I have set my base_controller as Login 我已将我的base_controller设置为Login
<?php
class Login extends CI_Controller {
/**
*
* load the magazines
*/
function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('User','National_Holiday','Updated_Holiday');
}
public function index() {
$this->load->view('login');
}
/**
*
* add a magazine
*/
public function login(){
$this->form_validation->set_rules(array (
array(
'field' => 'username',
'label' => 'username',
'rules' => 'required',
) ,
array(
'field' => 'password',
'label' => 'password',
'rules' => 'required|is_numeric',
),
));
$this -> form_validation ->set_error_delimiters('<div class="alert alert-error">','</div>');
if(!$this->form_validation->run()){
$this->load->view('login');
}
else {
$this->load->view('national_holiday_screen');
}
}
}
here is the view 这是视图
<?php echo validation_errors(); ?>
<form method="post">
<!-- LOGIN DIV STARTS HERE -->
<div>
<div> <h2> Log In </h2></div>
<div>
<lablel for="username"> Username </label>
<input type="text" name="username" value=""/>
</div>
<div>
<label for="password"> Password </label>
<input type="password" name="password" value=""/>
</div>
<div>
<br>
<input type="submit" value="Login">
</div>
</div>
<!-- LOGIN DIV ENDS HERE -->
</form>
When I click the login button, nothing happens. 当我单击登录按钮时,没有任何反应。 What am I doing wrong?
我究竟做错了什么?
You have to give action attribute in form tag, like this: action="http://yoursitename.com/controllername" in your case controllername is login. 您必须在表单标签中提供action属性,例如:action =“ http://yoursitename.com/controllername”(在您的情况下,controllername是login)。
For more help, you can refer: [ https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html][1] 要获得更多帮助,您可以参考:[ https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html] [1 ]
Hope this help! 希望有帮助!
this link shows how form_open()
works, one of codeigniter's utility functions from the form helper libary. 此链接显示
form_open()
工作方式,它是form_open()
的codeigniter的实用工具功能之一。
In your case you would want this line of code: 在您的情况下,您需要以下代码行:
<?php echo form_open('/login'); ?>
or something that you want to be the login url. 或您要用作登录网址的内容。
This line would replace 该行将替换
<form method="post">
in your html and when rendered would be something like 在您的html中,何时呈现将类似于
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/login" />
If you aren't familiar with URI routing, then you should read about that here]( https://ellislab.com/codeigniter/user-guide/general/routing.html ). 如果您不熟悉URI路由,则应在此处阅读有关内容]( https://ellislab.com/codeigniter/user-guide/general/routing.html )。 I would recommedn setting up a route for you login, but the default format for a url is
我建议您为登录设置路由,但网址的默认格式为
example.com/class/function/id/
so yours might look like 所以你的样子
example.com/login/login
And form_open()
would then look like (even though its kind of cluncky) 然后
form_open()
看起来像(尽管有点笨拙)
<?php echo form_open('/login/login'); ?>
You can try below code 您可以尝试以下代码
login.php controller file login.php控制器文件
<?php
class Login extends CI_Controller {
/**
*
* load the magazines
*/
function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->model('User','National_Holiday','Updated_Holiday');
}
public function index() {
$this->load->view('login');
}
/**
*
* add a magazine
*/
public function validate(){
$this->form_validation->set_rules(array (
array(
'field' => 'username',
'label' => 'username',
'rules' => 'required',
) ,
array(
'field' => 'password',
'label' => 'password',
'rules' => 'required|is_numeric',
),
));
$this -> form_validation ->set_error_delimiters('<div class="alert alert-error">','</div>');
if(!$this->form_validation->run()){
$this->load->view('login');
}
else {
$this->load->view('national_holiday_screen');
}
}
}
Here I have loaded one helper to give action url in view file 在这里,我已经加载了一个帮助程序,可以在视图文件中提供操作网址
$this->load->helper('url');
Also I have changed function name from login to validate as function name should not be similar to class name as constructor is already defined there 另外我已经从登录名更改了函数名以进行验证,因为函数名不应与类名相似,因为那里已经定义了构造函数
login.php view file login.php查看文件
<?php echo validation_errors(); ?>
<form method="post" action = "<?php echo site_url("login/validate"); ?>">
<!-- LOGIN DIV STARTS HERE -->
<div>
<div> <h2> Log In </h2></div>
<div>
<lablel for="username"> Username </label>
<input type="text" name="username" value=""/>
</div>
<div>
<label for="password"> Password </label>
<input type="password" name="password" value=""/>
</div>
<div>
<br>
<input type="submit" value="Login">
</div>
</div>
<!-- LOGIN DIV ENDS HERE -->
</form>
Hope this will help you 希望这个能对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.