I want to check username in the form valaible or not avalaible to create. Then i use ajax for check in database without refrash page. Then i create this, but the response always return false. I try only controller with print_r, the function cek_user() can get value true and false. Please help how to my ajax return false or true. Thank before.
I using ajax and CI
here my contoller User.php
public function cek_user()
{
$username = $this->input->post('user_name');
$query = $this->db->query("SELECT * FROM tb_user where username='".$username."'");
if($query->num_rows() > 0)
{
return false;
}
else
{
return true;
}
}
and this my form add_user.php
<div class="form-group col-md-6">
<div class="input-group">
<span class="input-group-addon">@</span>
<input id="username" name="username" type="text" class="form-control col-xs-6" placeholder="Username">
</div>
</div>
<div id="chk_username" class="form-group col-md-6">
</div>
and this my ajax script add_user.php
$('#username').change(function(){
var username = $('#username').val();
if(username != "")
{
$.ajax(
{
type : "POST",
url : "<?php echo base_url();?>user/cek_user",
data : { user_name:username },
dataType: "text",
success : function(response)
{
if (response == true)
{
$('#chk_username').html("<div class='btn btn-sm btn-block btn-success'>Username tersedia</div>");
console.log("true");
}
else if(response == false)
{
$('#chk_username').html("<div class='btn btn-sm btn-block btn-danger'>Username tidak tersedia</div>");
console.log("false");
}
}
});
}
});
how to get true or false value from this ajax script ?