[eluser]Isern Palaus[/eluser]
Hello,
I recently started coding AJAX (today) and I've decided to use jQuery. I don't use the library for CI because it had some troubles trying to configure it and it's easiest to talk of the jQuery JS files.
I'm trying to do a simple login (when it will be useful I'll write a few lines about to people who start coding AJAX) that checks the username/password. The problem is that it checks and EVERY TIME if it's correct or not returns me a "yes" on success. Here is the code:
The View One:
Code:
<?php
$this->load->view('baseHeader');
?>
<div id="contentlogin">
<?= form_open('user/login',array('id' => 'login')); ?>
<label for="username">Your Username:</label>
<input type="text" id="username" name="username" value="" class="text" /><br />
<label for="password">Password:</label>
<input type="password" id="password" name="password" value="" class="text" /><br />
<label></label>
<input type="submit" name="entrar" id="entrar" value="Login" /><br />
<?= form_close(); ?>
</div>
<div id="login_msg">
</div>
<?php
$this->load->view('baseFooter');
?>
Note: the baseHeader view includes the necessary jQuery files and the user.js I'm using.
user.js
Code:
$(document).ready(
function(){
$("#login").ajaxForm({
type: "POST",
url: "http://localhost/dalia/trunk/public_html/index.php/user/checkLogin",
dataType: "JSON",
data: "username="+$("#username").val()+"&password;="+$("#password").val(),
success: function(data){
$("#contentlogin").slideUp("slow",
function() {
// This effect it's not working too! :-(
$("#login_msg").fadeIn("slow",
function() {
$(this).html("Test");
}
);
}
);
// This IFs doesn't works... :S!
if(data.success == "yes") {
$("#login_msg").html("Esto ha validado");
}
if(data.success == "no") {
$("#login_msg").html("Esto NO ha validado");
}
}
});
}
)
The if(data.success... doesn't works too, anybody knows why?
The part of the controller that contains the function checkLogin:
Code:
function _check_login($username) {
$password = md5($this->validation->password);
$this->user_model->checkUserLogin($username,$password,"users");
}
function checkLogin()
{
$this->load->library('validation');
$rules['username'] = 'trim|required|callback__check_login';
$rules['password'] = 'trim|required';
$this->validation->set_rules($rules);
$fields['username'] = 'username';
$fields['password'] = 'password';
$this->validation->set_fields($fields);
if ($this->validation->run()) {
$output = "{success: 'yes', message: 'This is working'}";
} else {
$output = "{success: 'no', message: 'This isn't working'}";
}
$output = str_replace("\r", "", $output);
$output = str_replace("\n", "", $output);
echo $output;
}
The part of the model that checks the user login:
Code:
function checkUserLogin($username,$password,$table=users)
{
$query = $this->db->where("username",$username);
$query = $this->db->where("password",$password);
$query = $this->db->limit(1,0);
$query = $this->db->get($table);
if ($query->num_rows() == 0) {
$this->validation->set_message('_check_login', 'Invalid login');
return FALSE;
} else {
return TRUE;
}
}
Well, thanks in advance to everyone.
Regards,
-- Isern Palaus