使用ajax检查文本输入字段的值

I have a website where the log ins are screen names. On the create user form I want to be able to have ajax check if a screen name exists already as it is typed into the form.

This is the HTML form input field

<label for="screenName">Screen Name:
<input type="text" class="form-control" name="screenName" id="screenName" size="28" required>
<div class="screenNameError"></div>

A message should be displayed in the <div class="screenNameError"></div>line if the username matches the database.

This is my Jquery code for this.

$(document).ready(function(){

if ($('#screenName').length > 0){

    var screenName = $("input").keyup(function(){
        var value = $(this).val();
        return value;
    })

    $.ajax({
        type: 'post',
        url:  'screenNameCheck.php',
        data: 'Screen_Name=' + screenName,

        success: function (r) {
            $('.screenNameError').html(r);
        }
    })

}

});

This is the PHP file that gets called to make the DB query

$screenName = $_POST['Screen_Name'];

$screenNameSQL = "SELECT Screen_Name FROM Users WHERE Screen_Name = '$screenName'";
$result = $my_dbhandle->query($screenNameSQL); //Query database
$numResults = $result->num_rows; //Count number of results

$resultCount = intval($numResults);

if($resultCount > 0){
    echo "The username entered already exists. Please a different user name.";
}

For some reason my Jquery is not firing when I type the username in the form :/

Thanks in advance

Try changing your jQuery to this -

$(document).ready(function() {
  $('#screenName').keyup(function() {
    var value = $(this).val();
    $.ajax({
      type: 'post',
      url: 'screenNameCheck.php',
      data: 'Screen_Name=' + value,
      success: function(r) {
        $('.screenNameError').html(r);
      }
    });
  });
});

However you probably want to minimise the number of ajax requests being made so I would advise putting your ajax request into a setTimeout functon and clearing it with each subsequent keypress. -

$(document).ready(function() {
  var ajaxRequest;
  $('#screenName').keyup(function() {
    var value = $(this).val();
    clearTimeout(ajaxRequest);
    ajaxRequest = setTimeout(function(sn) {
      $.ajax({
        type: 'post',
        url: 'screenNameCheck.php',
        data: 'Screen_Name=' + value,
        success: function(r) {
          $('.screenNameError').html(r);
        }
      });
    }, 500, value);
  });
});

Your ajax call should be inside the keyup handler.

Add an event on keyup like this :

Edit

$("#screenName").on("keyup",function(){
  var screenName=$(this).val();

if(screenName!='')
{
 $.ajax({
        type: 'post',
        url:  'screenNameCheck.php',
        data: 'Screen_Name=' + screenName,

        success: function (r) {
            $('.screenNameError').html(r);
        }
    })
}
});

JsFiddle

if ($('#screenName').length > 0){

You should change it with

if ($('#screenName').val().length > 0){

OR

var name = $('#screenName').val();
if(name.length >0) {...

not sure about the syntax...