我一直在尝试使用 ajax 和 jquery 脚本检查电子邮件可用性,如下所示,
我的 Controller :
$get_result = $this->user->check_email_availablity();
if($get_result == FALSE ) {
$validate['message'] = '<p>Email is not available.</p>';
} else {
$validate['message'] = '<p>Email is available.</p>';
}
$this->load->view('user/signup', $validate);
我的模型:
function check_email_availablity() {
$email = $this->input->post('u_email');
$query = $this->db->query('SELECT u_email FROM tbl_users where u_email = "'.$email.'"');
if($query->num_rows() === 1) {
return FALSE;
} else {
return TRUE;
}
}
我的js:
$(document).ready(function() {
/// make loader hidden in start
$('#Loading').hide();
$('#email').blur(function(){
var a = $("#email").val();
var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
// check if email is valid
if(filter.test(a)){
// show loader
$('#Loading').show();
$.post("<?php echo base_url()?>main/signup", {
email: $('#email').val()
},
function(response){
//#emailInfo is a span which will show you message
$('#Loading').hide();
setTimeout("finishAjax('Loading', '"+escape(response)+"')", 400);
});
return false;
}
});
function finishAjax(id, response){
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
});
我的观点:
<?php echo form_input('u_email', set_value('u_email'), 'class="form-control" id="email"'); ?>
<span id="Loading"><?php echo $message; ?></span>
我的问题是模型始终返回 TRUE 并显示“电子邮件可用”消息,如何实时检查电子邮件可用性
最佳答案
在您的模型中更改查询行中的 if 条件:
function check_email_availablity() {
$email = $this->input->post('u_email');
$query = $this->db->query('SELECT u_email FROM tbl_users where u_email = "'.$email.'"');
if($query->num_rows() > 0) {
return FALSE;
} else {
return TRUE;
}
}
在你的 Controller 中:
$get_result = $this->user->check_email_availablity();
if(!$get_result) //if email already exist in your database
{
$validate['message'] = '<p>Email is not available.</p>';
} else {
$validate['message'] = '<p>Email is available.</p>';
}
$this->load->view('user/signup', $validate);
关于javascript - Codeigniter ajax 检查电子邮件可用性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31666884/