Hey I’m new to Codeigniter and I’m wondering why I’m having problems to combine codeigniter with jquery ajax form posts ..
this my form
test.php (views/test.php)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
[removed][removed]
[removed]
$(document).ready(function() {
$('#username').blur(function() {
var username = $("#username").val();
var dataString = 'username=' + username;
alert(username);
$.ajax({
type: "POST",
url: "/index.php/ajax/username_taken",
data: dataString,
success: function(msg)
{
$("#status").html(msg);
}
});
});
});
</script>
</head>
<body>
<form action="" method="post">
username: <input type="text" id="username" /> <br />
</form>
<div id="status"></div>
</body>
</html>
yeah I know I can user form helper, I’m just playing with it ...
and this is controller (controllers/ajax.php)
<?php
class Ajax extends CI_Controller {
public function username_taken()
{
$this->load->model('Login');
$username = trim($_POST['username']);
if ($this->Login->login_check($username))
echo "exists";
else
echo "not exists";
}
}
/* End of file ajax.php */
/* Location: ./system/application/controllers/ajax.php */
?>
and this is the model, login.php (models/login.php)
<?php
class Login extends CI_Model {
function login_check($username)
{
$this -> db -> select('id, username');
$this -> db -> from('users');
$this -> db -> where('username = ' . "'" . $username . "'");
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() >= 1)
{
return true;
} else
return false;
}
}
?>
now, I do getting the alerts with the username I’ve putted in, but I don’t get the result from the server side (exists / not exists) ... I wonder why, what I’m doing wrong.
thanks a lot for the help ![]()
