我想检查用户电子邮件是否已存在于数据库中。我为此构建了以下代码。
AJAX代码:
function emailCheck(){
console.log("hello");
var email = $("#email").val();;
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url(); ?>myCon/customerCheck',
data: {email:email},
success:function(response){
$('#test').html(response);
}
});
}
myCon控制器的功能:
public function customerCheck(){
$this->load->model('CustomerModel');
$res = $this->customerModel->customerMailCheck();
echo json_encode($res);
}
customerModel
模型的功能:
function customerMailCheck(){
$mail = $this->input->post('email');
$result = $this->db->get_where('privilege_customer', array('email' => $mail);
return $result->result();
}
现在每当我调用该函数时都会收到错误,指出内部服务器错误为500。
有没有更好的方法呢?
答案 0 :(得分:0)
模型函数中缺少一个括号):
试试这个:
sqlite
答案 1 :(得分:0)
您必须在控制器中收到ajax请求并将其传递给模型
myCon控制器
public function customerCheck(){
$this->load->model('CustomerModel');
-----> $mail = $this->input->post('email'); ------v
$res = $this->customerModel->customerMailCheck($mail);
echo json_encode($res);
}
型号:
function customerMailCheck($mail){
$result = $this->db->get_where('privilege_customer', array('email' => $mail);
return $result->result();
}
答案 2 :(得分:0)
试试这个
脚本:你错过了Bouble Qutes
function emailCheck(){
console.log("hello");
var email = $("#email").val();;
jQuery.ajax({
type: 'POST',
url: '<?=site_url()?>myCon/customerCheck',
data: {"email":email},
success:function(response){
$('#test').html(response);
}
});
}
模型:您错过了)
行
$result=
function customerMailCheck() {
$mail = $this->input->post('email');
$result = $this->db->get_where('privilege_customer', array('email' => $mail));
return $result->result();
}
答案 3 :(得分:0)
您可以针对您的问题尝试此解决方案。
请在页眉中添加以下代码
<script type="text/javascript">
base_url = '<?=base_url()?>';
</script>
更改控制器功能。
public function customerCheck(){
if ($this->input->is_ajax_request()) {
$this->load->model('CustomerModel');
$mail = $this->input->post('email');
$res = $this->customerModel->customerMailCheck($mail);
if(!empty($res)) {
$data['status'] = 'success';
$data['message'] = 'Email Adress is found';
} else {
$data['status'] = 'error';
$data['message'] = 'Data not found';
}
echo json_encode($data);
exit;
} else{
redirect('login/logout');
}
}
更改Ajax代码
function emailCheck(){
var email = $("#email").val();;
jQuery.ajax({
type: 'POST',
url : base_url +'myCon/customerCheck',
data: {email:email},
success:function(response){
if(response.status=="success"){
$('#test').html(response.message);
} else {
console.log(response.message)
}
}
});
}
CustomerModel模型的功能:
function customerMailCheck($mail){
$result = $this->db->get_where('privilege_customer', array('email' => $mail));
return $result->result();
}
我希望这会对你有所帮助。