任何人都可以使用ajax和代码点火器帮助我检查用户名是否在我的数据库中? 我不能使用form_validation方法,因为我有模态窗口干扰检查。
目前我的控制器如下:
function filename_exists(){
$username = $this->input->post('username');
$data['exists'] = $this->User_model->filename_exists($username);
}
我的模特:
function filename_exists($username)
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('username', $username);
$query = $this->db->get();
if ($query->num_rows() == 0) {
return true;
} else {
return false;
}
}
和我的ajax帖子:
function check_if_exists() {
<?php $username = $this->input->post('username');
?>
var username = '<?php echo $username ?>';
var DataString=$("#form1").serialize();
$.ajax({
url: "<?php echo base_url(); ?>index.php/Files/filename_exists/",
type: "post",
data: DataString + '&username=' + username,
success: function(response) {
if (response == true) {
$('#msg').html('<span style="color: green;">'+msg+"</span>");
}
else {
$('#msg').html('<span style="color:red;">Value does not exist</span>');
}
}
});
}
更新
<form name = "form1" id = "form1" method ="post"> <!--action="<?php echo base_url()."index.php/Admin/create_user"; ?>"-->
<?php echo validation_errors(); ?>
<label for="userID" class = "labelForm">User ID:</label>
<input type="text" id="userID" name="userID" class = "input2">
<label for="first_name" class = "labelForm">First Name:</label>
<input type="text" id="first_name" name="first_name" class = "input2">
<label for="last_name" class = "labelForm">Last Name:</label>
<input type="text" id="last_name" name="last_name" class = "input2">
<label for="username" class = "labelForm">Username:</label>
<input type="text" id="username" name="username" class = "input2" onblur="check_if_exists();">
<label for="password" class = "labelForm">Password:</label>
<input type="password" id="password" name="password" class = "input2" onblur="checkPasswords();">
<label for="passconf" class = "labelForm">Password:</label>
<input type="password" id="passconf" name="passconf" class = "input2" onblur="checkPasswords();">
<label for="email" class = "labelForm">Email:</label>
<input type="text" id="email" name="email" class = "input2">
<button type="button" id = "new_user_submit">Add New User</button>
答案 0 :(得分:2)
试试这个
在Ajax中
function check_if_exists() {
var username = $("#username").val();
$.ajax(
{
type:"post",
url: "<?php echo base_url(); ?>index.php/files/filename_exists",
data:{ username:username},
success:function(response)
{
if (response == true)
{
$('#msg').html('<span style="color: green;">'+msg+"</span>");
}
else
{
$('#msg').html('<span style="color:red;">Value does not exist</span>');
}
}
});
}
在控制器中
function filename_exists()
{
$username = $this->input->post('username');
$exists = $this->User_model->filename_exists($username);
$count = count($exists);
// echo $count
if (empty($count)) {
return true;
} else {
return false;
}
}
在模型中
function filename_exists($username)
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('username', $username);
$query = $this->db->get();
$result = $query->result_array();
return $result
}
答案 1 :(得分:1)
如果您只是想查看用户是否已经存在,则没有理由使用get()函数进行查询。只需执行count_all_results(),如果找到用户,它将返回一个数字,否则返回0。
function filename_exists($username) {
$this->db->where('username', $username);
return $this->db->count_all_results('users');
}
如果你的数据库中存在用户名,那么所有这一切都会返回一个大于零的数字。
答案 2 :(得分:0)
只是另一种结果相同的方法
<强> MODEL 强>
function filename_exists($username) {
$this->db->select()->from('users')->where('username', $username);
$query = $this->db->get();
return $query->first_row('array'); // returns first row if has record in db
}
<强> CONTROLLER 强>
function filename_exists() {
$username = $this->input->post('username');
$user = $this->user_modal->filename_exists($username);
return !empty($user); // not empty returns true else false
}
<强> AJAX 强>
在check_if_exists
功能中。
$.post('<?php echo base_url(); ?>index.php/files/filename_exists', { username: $('#username').val() }, function(response) {
var html = '<span style="color:red;">Value does not exist</span>';
if (response == true) {
html = '<span style="color: green;">' + msg + '</span>';
}
$('#msg').html(html);
});