1

I have this JQuery script which sends a request to check_username.php which then checks in my database if the username is available and echoes back true or false and it's all working.

However when I type into my form a username which doesn't exists the form turns green, which is good, but once I enter a username which exists it turns the form red and then no matter what username I type it will always stay red.

How to solve this?

jquery

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
  $(document).ready(function() {
        $("#username").keyup(function(e) {
              var username = $("#username").val();
                    $.post('check_username.php', {'username': username}, function(data) {
                          if(data=="true") {
                                $("#usergroup").addClass("input-group has-error");
                          } else if(data=="false"){
                                $("#usergroup").addClass("input-group has-success");
                          }
                    });
        });
  });
</script>

check_username.php

<?php
include 'includes\\connect.inc.php';

if(isset($_POST['username'])) {
      $username = $_POST['username'];
            $query = "SELECT id FROM users WHERE username='$username'";
            $query_run = mysql_query($query);
                  if($query_run) {
                        $query_num_rows = mysql_num_rows($query_run);
                              if($query_num_rows==1) {
                                    echo "true";
                              } elseif($query_num_rows==0) {
                                    echo "false";
                              }
                  }
}

?>

connect.inc.php

<?php
$mysql_error = mysql_error();

$mysql_database = 'codeforum';
$mysql_host = '127.0.0.1';
$mysql_user = 'root';
$mysql_pass = '';

if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass) || !mysql_select_db($mysql_database)) {
    die($mysql_error);
}
 ?>
Mauker
  • 11,237
  • 7
  • 58
  • 76
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 07 '15 at 17:38
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Aug 07 '15 at 17:38
  • I will deal with those things later, now I need to figure out how to get it working and its kinda hard for me, I know very little jquery – coder3000php Aug 07 '15 at 17:39
  • If you don't have time to do it right the first time, when will you find the time to do it over? ¯\\_(ツ)_/¯ When your form gets focus clear all of the classes. – Jay Blanchard Aug 07 '15 at 17:40
  • Figured it out, using the focus event didnt do exactly what I wanted, but the event keydown fixed it! – coder3000php Aug 07 '15 at 17:50
  • 1
    Then answer your own question ;) – Mauker Aug 07 '15 at 18:51

1 Answers1

0

Figured it out, using the focus event didnt do exactly what I wanted, but the event keydown fixed it!