[英]How to I check if email exist in user table with codeinighter and ajax
我希望用戶輸入他們的電子郵件,單擊一個按鈕,然后檢查該電子郵件是否已存在於數據庫中,這是我迄今為止嘗試的未成功的方法
模型
public function email_exists($email) {
$this->db->select('*');
$this->db->from('users');
$this->db->where('email', $email);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
控制者
function email_exists(){
$this->load->model('main_model');
$email = $this->input->post('email');
$exists = $this->main_model->email_exists($email);
$count = count($exists);
echo $count;
if (empty($count)) {
return true;
} else {
return false;
}
}
查看即時通訊將在視圖中林不知道,Ajax代碼,如果這是正確的
<input id="about-email" type="email">
<div id="about-you" >enter</div>
<script>
var email = $("#about-email").val();
$('#about-you').click(function() {
$.ajax({
type:"post",
url: "<?php echo base_url(); ?>index.php/my_controller_name/email_exists",
data:{ email:email},
success:function(response)
{
if (response == true) {
$('#msg').html('<span>email exists</span>');
} else {
$('#msg').html('<span>Value does not exist</span>');
}
}
});
});
</script>
上面的ajax代碼破壞了該頁面的所有JavaScript,而且我還在同一頁面上進行了另一個ajax調用,因此我不知道是否可以在一個頁面上進行2個ajax調用。
您的ajax應該是這樣的:
<script>
var email = $("#about-email").val();
var url = <?= base_url("my_controller_name/email_exists") ?>
$('#about-you').click(function() {
$.ajax({
type:"post",
url: url,
dataType: "json",
data: {email: email},
success:function(response)
{
if (response) {
$('#msg').html('<span>email exists</span>');
} else {
$('#msg').html('<span>Value does not exist</span>');
}
}
});
return false;
});
</script>
在您的控制器中:
function email_exists(){
$email = $this->input->post('email');
$this->load->model('main_model');
$exists = $this->main_model->email_exists($email);
$exists = (count($exists) > 0)? true : false;
echo json_encode($exists);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.