将预期结果从CI_Controller函数转移到Ajax函数的问题

时间:2018-12-13 06:01:15

标签: jquery ajax codeigniter

我不知道我的代码有什么问题。调用该函数时,无法从CI_Controller获得预期的输出。我怀疑我的CI_Controller没有向我的Ajax函数返回任何值。

这是我的CI_Controller函数

public function check_student_id($data){
   $result = $this->page_model->check_id($data);
   return $result;
}

然后是CI_Model函数

public function check_id($data){
   $this->db->where('student_id', $data);
   $result = $this->db->count_all_results('users');
   if($result > 0){
     return true;
   }
   else{
     return false;
   }
}

最后是Ajax功能

$.ajax({
    url: $('#base_url')+"pages/check_student_id/"+student_id,
    method: "POST",
    data: {'student_id':student_id},
    success: function(data){
      if(data == true){
        $('#student_id_error_message').show();
        $('student_id_error_message').text('Student ID has been already registered.');
        $('#student_id_error_message').addClass('text-danger font-italic my-0');
      }
      else{
        $('#registerForm').hide(600);
        $('#registerSuccess').text('You have successfully registered!');
        $('#registerSuccess').addClass('text-success py-3');
        $('#registerSuccess').show(600);
      }
    }
  })

请帮助我。先感谢您。祝你有美好的一天!

1 个答案:

答案 0 :(得分:0)

您应该在控制器的函数中回显$ result而不是返回值。

public function check_student_id($data){
   $result = $this->page_model->check_id($data);
   echo $result;
}
相关问题