Hello all,
I’m a newbie to CodeIgniter. I’m having some trouble with my form validation. My form allows a user to enter a username and password then checks it against a database. If the username or password do not match the program displays an error message. If the username and password do match then they are redirected to a page called my_blog.
Here’s the code for the controller:
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('log_in');
}
else
{
$this->load->view('my_blog');
}
}
function user_not_exists($username) {
$this->load->model('log_in_db');
$user_check = $this->user_model->user_not_exists($username);
if($user_check > 0) {
redirect('my_blog');
return TRUE;
}
else {
$this->form_validation->set_message('required', 'Username does not match');
return FALSE;
}
}
function password_not_exists($password) {
$check_password = $this->user_model->password_not_exists($password);
if($check_password > 0) {
redirect('my_blog');
return TRUE;
}
else {
$this->form_validation->set_message('required', 'Password does not match');
return FALSE;
}
}
}
Here’s the code for the model which the controller links to called log_in_db:
class log_in_db extends CI_Model {
function user_not_exists($username) {
$this->db->where('username', $username);
$query = $this->db->get('users');
return $query->num_rows();
}
function password_not_exists($password) {
$this->db->where('password', $password);
$query = $this->db->get('users');
return $query->num_rows();
}
}
And finally here’s the code for the log in form which holds the actual form including both textfields for username and password:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html >
<head>
<meta http-equiv="Content-Type" c charset=iso-8859-1" />
<title>Log in form</title>
<link rel="stylesheet" href="../../../_css/style-sheet.css" media="screen" />
</head>
<body>
<div id="login-form">
<div id="log-in-form">
<?php echo validation_errors(); ?>
<?php echo form_open('my_form'); ?>
<table>
<tr>
<td class="log-in-td" align="left">
<label for="username" class="login-text">Username</label>
</td>
</tr>
<tr>
<td class="log-in-td" align="left">
<input name="username" type="text" size="25" maxlength="25" id="username" class="my-textfields" /> </td>
</tr>
<tr>
<td class="log-in-td" align="left">
<label for="password" class="login-text">Password</label>
</td>
</tr>
<tr>
<td class="log-in-td" align="left">
<input name="password" type="password" size="25" maxlength="25" id="password" class="my-textfields" /> </td>
</tr>
<tr>
<td align="center" class="log-in-td">
<input type="button" id="My-Log-in-button" value="log-in" />
</td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
</div>
</body>
</html>
If anyone can give me any help to why the error messages aren’t displayed or the user isn’t redirected when they enter a correct username and password. Thanks in adavance.