1

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

4
  • 1
    what the result of your console.log(html)? Commented Jul 29, 2016 at 8:52
  • 1
    Bobby Tables! Commented Jul 29, 2016 at 8:53
  • For security reasons: you really should escape your username. Currently your application is vulnerable for SQL injections. Commented Jul 29, 2016 at 8:53
  • 1
    Apart from that, your logic is wrong: rowCount()>0 should return "Exists" Commented Jul 29, 2016 at 8:56

4 Answers 4

1

Please try following code.

JavaScript code

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

And you've mentioned id="username " (with space), please change that like as below.

<input type="text"  class="form-control col-md-7 col-xs-12"  name="username" id="username">
Sign up to request clarification or add additional context in comments.

1 Comment

@johansson, you accepted my answer and then removed it.
1

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.

Comments

0

This should be your code:

$('document').ready(function(){
            $('#username').blur(function(){

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

Comments

0

looks Jquery syntax error ...

<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)
                    {
                        console.log(html);
                        $('#availablity').html(html);

                    },
                    error: function(html)
                    {
                        console.log(html);
                    }
                });
            });
    });
</script>

check your console message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.