Im trying to get starting with JSON and jQuery. I'm not a JavaScript programmer at all so I'm mixing and matching and having quite a few issues getting my code to work. I thought someone might be able to help me out.
I have a model which uses the following PHP code to check if an email address is registered with the system:
function is_email_available($email) {
$this->db->select('1', FALSE);
$this->db->where('LOWER(email)=', strtolower($email));
$this->db->or_where('LOWER(new_email)=', strtolower($email));
$query = $this->db->get($this->table_name);
return $query->num_rows() == 0;
}
The above code is being called by my ajax controller using the following, and if it is registered returns a json true or false
function email_availability() {
// Check to see if the username is available or taken
if ( $this->users->is_email_available( $this->input->post('emailaddress'))) {
echo json_encode(TRUE);
} else {
echo json_encode(FALSE);
}
}
The above is called from my JS file using the following jQuery, but it doesn't seem to be working at all.
if(iEmail.val()) {
$.ajax({
url: '/ajax/email_availability',
data: 'emailaddress=' + iEmail.val(),
dataType: 'json',
type: 'post',
success: function(msg) {
iEmail.next('.error').text('This Email Address is not registered');
error = true;
}
});
}
I think my jQuery is completely off but can't figure it out. Any help would be greatly appreciated.