JQuery Username Validation

后端 未结 5 1644
执念已碎
执念已碎 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:45

    Try changing the PHP source to return "1" or "0":

    include('database_connection.php');                                  
    if (isset($_POST['username'])) {                                                               
        $username = mysql_real_escape_string($_POST['username']);                                  
        $check_for_username = mysql_query("SELECT user_id FROM users WHERE username='$username'"); 
        if (mysql_num_rows($check_for_username)) {
            echo "TRUE";                                                                           
        } else {
            echo "FALSE";                                                                           //No Record Found - Username is available
        }
    }
    ?>
    

    and then change your JS to parse that as an integer:

    success:
     function(msg) { 
       isSuccess = parseInt(msg);
     }
    

提交回复
热议问题