我有控制器文件:- login.php
class Login extends CI_Controller {
function __construct() {
parent::__construct();
}
function success() {
$username = $this->input->post('username');
$password = $this->input->post('password');
$errorMsg ="";
$queryResult = $this->logins->validate($username,$password);
if($queryResult == TRUE) {
redirect ('home');
}
else {
$errorMsg ="Invalid Username or Password";
$this->load->view('login',$errorMsg);
}
}
}
查看:- login.php
<script type="text/javascript">
function validatelogin(){
var x=document.forms["login"]["username"].value;
var y=document.forms["login"]["passwrd"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
if (y==null || y=="")
{
alert("Password field must be filled out");
return false;
}
/*if(x!="monisha" && y!="monisha"){
alert("Username and Password incorrect");
return false;
}*/
return true;
}
</script>
HTML 表单有:-
<form name="login" id="login" action="<?php echo base_url() ?>login/success" onsubmit="return validatelogin()" method="post">
模型文件 logins.php 具有功能,描述功能验证
class Logins extends CI_Model {
function __construct()
{
parent::__construct();
}
function validate($username,$password){
$this->db->select('username','password');
$this->db->from('logins');
$this->db->where('username', $username);
$query = $this->db->get('logins');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$passwrd = $row->password;
if($passwrd == $password) {
return TRUE;
}
}
} else {
return FALSE;
}
}
}
但显示错误:-
致命错误:在此行中的非对象上调用成员函数 validate():-
$queryResult = $this->logins->validate($username,$password);