johansson johansson - 4 months ago 6
PHP Question

Check username with ajax jquery php mysql

I have table users :


  • id : int,

  • username : varchar(255),

  • password :varchar(255),



I would check if the login exist in the database or not

I try with this code jquery and html :

<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">username <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" class="form-control col-md-7 col-xs-12" name="username " id="username ">
<span id="availablity"></span>
</div>
</div>
<script type="text/javascript">

$('document').ready(function(){
$('#username').blur(function(){
var username = $(this).val();
$.ajax ({
url = "check.php",
method = "POST",
data = {username :username },
dataType : "text",
success:function(html)
{
$('#availablity').html(html);
}
});
});
});
</script>


page check.php :

if(isset($_POST['username'])){
$username = $_POST["username"];
$sql = $db->query("SELECT * FROM users WHERE username = '$username'");
if($sql->rowCount() > 0){
echo '<span class="text-danger">Not Exist</span>';
}else{
echo '<span class="text-success">Exist</span>';
}

}


But no results

Answer

You have used = in your ajax configuration. Thats an syntax error. Use

$.ajax({
    url: "check.php",
    method: "POST",
    data: {username: username},
    dataType: "text",
    success: function (html) {
        $('#availablity').html(html);
    }
});

instead.