[eluser]ninjayan[/eluser]
Hi everyone. I'm just new to CodeIgniter. I was following different tutorials creating a form with validation and now, I want to create a form which utilizes jQuery+Ajax. So I have this code right now.
view: login
Code:
[removed]
$(document).ready(function() {
$('#login-form').on('submit', function() {
$('.error-messages p').remove();
var login_form = $(this);
$.post(
login_form.attr('action'),
login_form.serialize(),
function(data) {
if(data) {
$('.error-messages').slideDown('fast').append(data);
}
else {
[removed].href = "home.php";
}
}, 'json'
);
return false;
});
});
[removed]
I will not be posting the form, just the javascript code
controller: main/login_validation
Code:
public function login_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
if ($this->form_validation->run() === FALSE) {
$data = validation_errors();
}else {
$this->load->model('user_model');
$user = $this->user_model->validate();
if ($user) {
redirect(base_url('site/dashboard'));
}else {
echo '<p>Incorrect username and/or password.</p>';
}
}
echo json_encode($data);
}
model: user_model
Code:
public function validate() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('users');
if ($query->num_rows() == 1) {
foreach ($query->result() as $row) {
if ($row->status == 'Pending') {
redirect(base_url('main/login_pending'));
}elseif ($row->status == 'Disabled'){
redirect(base_url('main/login_disabled'));
}else {
//SET THE SESSION
$user_info = array(
'username' => $row->username,
'first_name' => $row->first_name,
'middle_initial' => $row->middle_initial,
'last_name' => $row->last_name,
'office' => $row->office,
'privilege' => $row->privilege,
'status' => $row->status,
'is_logged_in' => 1
);
$this->session->set_userdata($user_info);
return true;
}
}
}else {
return false;
}
}
-----
Now, when I click login without inputs or any of the fields the validation is ok. If the user is not found, it also returns a message and it is ok. But my problem is, when the user is validated and exist, it should redirect to
redirect(base_url('site/dashboard')); but its not. Please help me. Thanks!