简体   繁体   中英

check username availability with jQuery

I am using this function to check username availability when registering, and if the username exist, the submit button is disabled. I'm using a php script to check if the username exists in the database. The code works perfectly in my local server, but when hosted on a Godaddy server, I get the below error when I submit or when I try to navigate to another page:

空响应

After the empty response, I can't access my website for about a minute and then it's fine.

I tried a lot of things like calling jquery.js file before all other js files, but nothing works for me.

I am not very good with jQuery and javascript. Thank you for your suggestions.

ere is the script and the HTML form :

 function check_availability_username() { var min_chars = 4; var max_chars = 30; var re = /^\\w+$/; var characters_error1 = 'Minimum amount of chars is 4'; var characters_error2 = 'Maximum amount of chars is 30'; var characters_error3 = 'Chars can be only letters, numbers and underscores!'; var checking_html = 'Checking...'; var username = $('#username').val(); $.post("check_username.php", { username: username }, function(result) { if (result == 1) { if ($('#username').val().length < min_chars) { $('#username_availability_result').html(characters_error1); $('input[name=submit]').attr('disabled', true); } else if ($('#username').val().length > max_chars) { $('#username_availability_result').html(characters_error2); $('input[name=submit]').attr('disabled', true); } else if (!re.test($('#username').val())) { $('#username_availability_result').html(characters_error3); $('input[name=submit]').attr('disabled', true); } else { $('#username_availability_result').html(username + ' is Available'); $('input[name=submit]').attr('disabled', false); } } else { $('#username_availability_result').html(username + ' is not Available'); $('input[name=submit]').attr('disabled', true); } }); if (event.which == 13 || event.keyCode == 13) { $.post("check_username.php", { username: username }, function(result) { if (result == 0) { $('#username_availability_result').html(username + ' is not Available'); $('input[name=submit]').attr('disabled', true); } }); } $('#submit').click(function() { $.post("check_username.php", { username: username }, function(result) { if (result == 0) { $('#username_availability_result').html(username + ' is not Available'); $('input[name=submit]').attr('disabled', true); } }); }); } 
 <form id="login-id" action="suscribe-form.php" method="POST" onkeyup="return check_availability_username();"> <div class="form-group"> <label for="username">Username</label> <input type="text" class="form-control button-or-space" name="username" id="username" value="" minlength="4" maxlength="25" required pattern="\\w+"> <input type='hidden' id='check_username_availability' value='Check Availability'>&nbsp;<span id='username_availability_result' name='username_availability_result'></span> </div> <div class="form-group"> <label for="password1">Password</label> <input type="password" class="form-control button-or-space" name="password1" id="password1" value="" minlength="8" maxlength="60" required pattern="(?=.*\\d)(?=.*[az])(?=.*[AZ]).{8,}" onchange="form.password2.pattern = this.value;"> </div> <div class="form-group"> <label for="password2">Confirm password</label> <input type="password" class="form-control button-or-space" name="password2" value="" minlength="8" maxlength="60" required pattern="(?=.*\\d)(?=.*[az])(?=.*[AZ]).{8,}"> </div> <div class="form-group pull-right"> <input type="submit" name="submit" id="submit" value="Register" class="btn btn-success button-or-space"> </div> </form> 

And here is the php script :

<?php    

mysql_connect('localhost', 'root', '');
mysql_select_db('DB');

$username = mysql_real_escape_string($_POST['username']);

$result = mysql_query('select username from user where username = "'.$username.
  '"');

if (mysql_num_rows($result) > 0) {
  echo 0;
} else {
  echo 1;
}

?>

I finally found the solution. The problem was not from the jQuery or the php script, i only changed the way i call the function in the form from : onkeyup to onchange, and it is now working like a charm. I hope this tip avoid to others losing hours of time like it did to me. Thank's guys for your suggestions.

If you are getting 500 error that there is problem with the database connection. check for the included files and connection variables used for database connection.

Hope that will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM