johansson johansson - 1 year ago 56
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>
<script type="text/javascript">

var username = $(this).val();
$.ajax ({
url = "check.php",
method = "POST",
data = {username :username },
dataType : "text",

page check.php :

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


But no results

Answer Source

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

    url: "check.php",
    method: "POST",
    data: {username: username},
    dataType: "text",
    success: function (html) {


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download