任何人都可以帮助我检查用户名是否在我的数据库中使用ajax和代码点火器? 我无法使用form_validation方法,因为我的模态窗口会干扰检查。用codeigniter和ajax检查数据库中是否存在用户名
目前我的控制器看起来像:
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>');
}
}
});
}
UPDATE
<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>
将'$ query-> num_rows()== 0'更改为'$ query-> num_rows()> 0'并测试 –
'print_r($ username)'在控制器中并且检查数据即将到来 –
@Abdulla当我尝试打印用户名什么都没有出现,但如果我在console.log(DataString)成功部分的ajax后它告诉我我进入了什么? – user