0

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.

5
  • where is the error? in the request part or in the response? check in firebug if the request to your php page is actually sent Commented Mar 6, 2013 at 15:32
  • @gpasci - Its just returning 'TRUE' if the email address entered is not in the database Commented Mar 6, 2013 at 15:35
  • You don't need to use json_encode( true). instead you can just echo 1 or 0. Another thing do you get any error? Where? Have you tried debugging your code with firebug or the developer tools from chrome? Commented Mar 6, 2013 at 15:38
  • post console.log(msg) log inside success() callback Commented Mar 6, 2013 at 15:43
  • It says error, but the page reloads before i can check it. Commented Mar 6, 2013 at 15:48

1 Answer 1

1

You didn't really check if the result was true or false.

        success: function(msg) {
            if (msg){
                //email is available
            }
            else{
                //email is not available
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.