Ajax just means your login controller will return something machine readable (json/xml) rather than html.
Your controller really just needs to set a session variable.
class Login extends Controller {
function __construct() {
parent::Controller();
$this->load->model('user_m');
}
function json_login() {
$user = $this->user_m->get_user($_POST['username']);
if($user->password == md5($_POST['password'])) {
$this->session->set_userdata('username',$user->username);
print json_encode($this->user);
}
else {
print json_encode(FALSE);
}
return;
}
function logout() {
$this->session->destroy();
redirect('/theloginpage');
}
}
Assuming you are using jquery for the javascript the ajax call could work something like this:
$('#login_form').submit(function(event) {
event.preventDefault();
$.ajax({
type:"POST",
data:$(this).serialize(),
dataType:"json",
url:"/login/json_login",
success: function(data) {
if(data['username'] != null) {
$(this).append('<p>You are now logged in</p>');
reload_table_data();
}
else if(data == flase){
$(this).append('<p>Fail whale, wrong username or password</p>');
}
}
});
}
Now when displaying the data check if the 'username' has been set in the session. Also don't forget to create a logout method which destroys the session. You can return the table data as an html snippet and insert it straight into the DOM or json/xml loop though it.
The login form could be displayed anywhere on the page and there are plenty of libraries for jquery, dojo, prototype etc which will provide a dialogue box.