Hello, I want to ask, what going on for this. I try to implement form, user key in the username, it will trigger the function to check the username is existing or not.
I tried to figure out the problem, but dont know what's going on...
My Js file,
jQuery(document).ready(function() {
$('.register form').submit(function(){
$(this).find("label[for='username']").html('Username');
var username = $(this).find('input#user_name').val();
if(username != ''){
$.ajax({
url: 'checkuser.php',
type: 'POST',
dataType: 'json',
data: {user_name: username},
beforeSend: function(){
console.log(username);
},
success: function (data) {
console.log(data);
if(data.success==true){
$(this).find("label[for='username']").append("<span style='display:none; font-size:12px;' class='red'> - ok</span>");
$(this).find("label[for='username'] span").fadeIn('medium');
return false;
}
else{
$(this).find("label[for='username']").append("<span style='display:none; font-size:12px;' class='red'> - failed</span>");
$(this).find("label[for='username'] span").fadeIn('medium');
return false;
}
}
}).fail(function() {
$(this).find("label[for='username']").append("<span style='display:none; font-size:12px;' class='red'> - error</span>");
$(this).find("label[for='username'] span").fadeIn('medium');
return false;
});
}
});
});
}
my CheckUser.php
<?php
error_reporting(0);
include 'dbc.php';
foreach($_GET as $key => $value) {
$get[$key] = filter($value);
}
foreach($_POST as $key => $value) {
$data[$key] = filter($value);
}
$user = $data['user_name'];
$rs_duplicate = mysqli_query($link, "select count(*) as total from users where user_name='$user' ");
if(!$rs_duplicate)
echo("Error description: " . mysqli_error($link));
else{
list($total) = mysqli_fetch_row($rs_duplicate);
if($total > 0) {
$return['success'] = false;
$return['msg'] = "Username already exists.";
//echo '<span class="error" style="font-size:14px;">Username already exists.</span>';
//exit;
}
else if(strlen($user) == 0){
$return['success'] = false;
$return['msg'] = "exxsxx";
//echo '<span class="error"></span>';
}
else {
$return['success'] = false;
$return['msg'] = "Use alphanumeric characters only.";
//echo '<span class="error" style="font-size:14px;">Use alphanumeric characters only.</span>';
}
}
echo json_encode($return);
?>