我尝试在Codeigniter中使用Ajax检查用户名是否可用。但是没有用。我已经尝试了许多教程,但是仍然无法正常工作。我的下面的代码有什么问题吗?
ajax:
$('#username').change(function(){
var username = $('#username').val();
if(username != ''){
$.ajax({
url:"<?php echo base_url('staff/admin/checkUsername');?>";
method:"post",
data:{username:username},
success:function(data){
$('#username_result').html(data);
}
});
}
}
控制器:
function checkUsername($userName){
if ($this->m_admin->checkUserexist($userName) == false) {
return true;
} else {
return false;
}
}
型号:
function checkUserexist($userName) {
$this->db->where('username', $userName);
$this->db->from('tbuser');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return true;
} else {
return false;
}
}
答案 0 :(得分:0)
尝试使用Ajax代码
$('#username').change(function(){
var username = $('#username').val();
if(username != ''){
$.ajax({
beforeSend: function () {
},
complete: function () {
},
url:"<?php echo base_url('staff/admin/checkUsername');?>";
method:"post",
dataType: "json",
data:({username:username}),
success:function(data){
//$('#username_result').html(data);
alert(data.status)
},
error: function (data) {
}
});
}
}
在控制器中尝试
function checkUsername($userName){
if ($this->m_admin->checkUserexist($userName) == false) {
$response_array['status'] = true;
echo json_encode($response_array);
exit();
} else {
$response_array['status'] = false;
echo json_encode($response_array);
exit();
}
}
答案 1 :(得分:0)
It works fine for me.Set input textbox id and name as same...
Html :
<form id="myform" method="POST">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
</div>
<div class="checkbox">
<label><input type="checkbox" name="remember"> Remember me</label>
</div>
<button type="submit" id="submit_btn" class="btn btn-default">Submit</button>
</form>
Codeigniter validation :
public function validate()
{
$json = array();
$this->form_validation->set_rules('email','Email', 'required');
$this->form_validation->set_rules('pwd','Password', 'required');
if ($this->form_validation->run() == FALSE) {
$json = array(
'success' => 0,
'message' => 'Error occured',
'error' => $this->form_validation->error_array()
);
} else {
$data['email']=$this->input->post('email');
$data['pwd']=$this->input->post('pwd');
$json = array(
'success' => 1,
'message' => 'Data inserted successfully'
);
}
$this->output->set_content_type('application/json')->set_output(json_encode($json));
}
Ajax call :
<script type="text/javascript">
$(document).on('click', '#submit_btn', function() {
event.preventDefault();
var dataString = $("#myform").serialize();
var url="welcome/validate"
$.ajax({
type:"POST",
url:"<?php echo base_url() ?>"+url,
data:dataString,
success: function(json) {
try {
if (json['success'] == 1) {
alert(json['message']);
} else {
$('.error').hide();
var len = Object.keys(json['error']).length;
var error = json['error'];
if (len > 0) {
$.each(error, function( index, value ) {
if (value) {
$('#'+index).after('<p class="error">'+value+"</p>");
}
});
}
}
} catch (e) {
console.log('Exception while request..');
}
},
error: function() {
console.log('Error while request..');
}
});
})
</script>
答案 2 :(得分:0)
首先,不要使用过多的true
和false
第二,您没有在来自ajax请求的控制器中获得$userName
控制器
function checkUsername($userName){
$userName = $this->input->post('username');
$if_exists = $this->m_admin->checkUserexist($userName);
if ($if_exists > 0) {
echo json_encode('Exists');
} else {
echo json_encode('Not exists');
}
}
模型
检查行数
function checkUserexist($userName) {
return $this->db->get_where('tbuser', ['username' => $userName])->num_rows();
}
Ajax
$('#username').change(function(){
var username = $('#username').val();
if(username != ''){
$.ajax({
url:"<?php echo base_url('staff/admin/checkUsername');?>";
method:"post",
data:{username:username},
dataType: 'json',
success:function(data){
console.log(data); //Exists OR Not exists
}
});
}
}