JQuery Username Validation

后端 未结 5 1649
执念已碎
执念已碎 2020-12-18 16:39

I\'m new to JQuery and attempting to write a script to check username availability. My problem is that no matter what I type, I always get back \"This username is already in

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 17:22

    You could use the following, just change it for your user name (I use the email address as the user name. I use an empty table cell id="emailError" to hold the message.

    The jQuery:

        $('#email').keyup(function(){
          $.ajax({
            type: 'POST',
            url: 'ajax.checkEmail.php',
            data: {e:$('#email').val()},
            success: function(data){
              if(data == 'true'){
                $('#emailError').html('This email is already used');
              }else{
                $('#emailError').html('');
              }
            }
          })
        });
    

    The PHP (ajax.checkEmail.php):

    
      include('inc.php');
    
      $query  = 'SELECT COUNT(*) FROM tblcustomers WHERE email = "'.$_POST['e'].'"';
      $result = $dbO->getSingleResult($query);
    
      if($result > 0)
        print 'true';
      else
        print 'false';
    
    

提交回复
热议问题