检查用户名是否唯一或是否在Codeigniter中不使用Ajax [英] check username is unique or not using ajax in codeigniter
问题描述
直接指向。
我正在检查输入的用户名在数据库中是否存在。因此,我决定使用ajax。
Direct to the point. I am checking that my entered username exists or not exist in the database. So I have decided to use ajax.
预期结果就像当我输入已经存在的用户名时,在它下面出现div样式更改和错误消息出现,但是现在不打h,我不知道为什么?
The expected result is like when I enter the username which already exists then the div style get change and error message get appear under it but this is not happing now I don't know why?
我对ajax并不熟悉,因此如果您发现语法缺失或技术错误,就可以指出来。
I am not familiar with ajax so any syntax missing or any wrong technique you see then you can point it out.
这是我的控制器功能:
function check_userName_exists()
{
$user_name = $this->input->post('user_name');
log_message('debug', 'Posted Username by Ajax for Check it exist or not : '. print_r($user_name, TRUE));
$exists = $this->student_db->check_user_name($user_name);
log_message('debug', 'Username Exist or not Response array: '. print_r($exists, TRUE));
$count = count($exists);
// echo $count
log_message('debug', 'Username Exist or not Response Count: '. print_r($count, TRUE));
if ($count == 1) {
$data['unique'] = 1;
print(json_encode($data));
} else {
$data['unique'] = 0;
print(json_encode($data));
}
}
这是我的模型函数:
function check_user_name($user_name)
{
$this->db->select('user_name');
$this->db->from('user');
$this->db->where('user_name', $user_name);
$query = $this->db->get();
$query_last = $this->db->last_query();
log_message('debug', 'Find UserName is Unique Or Not Query: '. print_r($query_last, TRUE));
$result = $query->result_array();
log_message('debug', 'Find UserName is Unique Or Not Result: '. print_r($result, TRUE));
return $result;
}
这是Ajax脚本:
<script type="text/javascript">
function check_if_exists() {
var user_name = $("#user_name").val();
// console.log(user_name);
$.ajax({
type:'POST',
url: "<?php echo site_url('student/check_userName_exists'); ?>",
data:{ 'user_name':user_name},
success:function(response)
{
if (response.unique = 1)
{
// alert(success);
//console.log(response);
$("#userNameDiv").addClass("has-warning");
$("#user_name").addClass("form-control-danger");
$("#userNameErrorMsg").text("This User Name Is Already Takken. Please Try Something Else.");
}
else
{
$("#userNameDiv").removeClass("has-warning");
$("#user_name").removeClass("form-control-danger");
$('#userNameErrorMsg').text('');
}
}
});
};
这是我的HTML:
<div class="form-group" id="userNameDiv">
<input type="text" class="form-control form-control-success" name="user_name" required id="user_name" placeholder="Enter Username" onblur="check_if_exists();">
<small class="text-help" id="userNameErrorMsg"></small>
</div>
在控制台中获得空白响应。
got blank response in console.
推荐答案
您的控制器函数总是返回0。Count返回一个整数,您正在检查它是否为空...更新了下面的控制器代码以解决此问题。
Your controller function is always going to return 0. Count returns an integer, you're checking if it's empty... Updated the controller code below to fix that.
Ajax期望json响应不是单个值。
Ajax expects a json response not a single value.
if ($count > 0) {
$data['unique'] = 1;
print(json_encode($data));
} else {
$data['unique'] = 0;
print(json_encode($data));
}
然后在ajax函数中
if (response.unique == '1')
这篇关于检查用户名是否唯一或是否在Codeigniter中不使用Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!