0

我试图从 JSON 和 jQuery 开始。我根本不是 JavaScript 程序员,所以我正在混合和匹配,并且在让我的代码正常工作时遇到了很多问题。我想有人可能会帮助我。

我有一个模型,它使用以下 PHP 代码来检查电子邮件地址是否已在系统中注册:

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;
}

我的 ajax 控制器使用以下代码调用上面的代码,如果已注册,则返回 json true 或 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);
  }

}

上面是使用以下 jQuery 从我的 JS 文件中调用的,但它似乎根本不起作用。

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;
    }
  });
}

我认为我的 jQuery 完全关闭但无法弄清楚。任何帮助将不胜感激。

4

1 回答 1

1

您并没有真正检查结果是真还是假。

        success: function(msg) {
            if (msg){
                //email is available
            }
            else{
                //email is not available
            }
        }
于 2013-03-06T15:29:02.140 回答