Jump to content

Username availability button


chao

Recommended Posts

I'm making a registration form with a username field. I'd like an AJAX button to check if an entered username is available. The usernames are stored in a MySQL database.

 

Here's an example of such a button: http://www.youtube.com/signup?next=/index

 

I found some code online, but it's excessive and difficult to read. I'd like to make a simple one myself. Do you have any guides or recommendations? Thanks.

Link to comment
Share on other sites

this needs two parts...first is a server side script (probably PHP) that receives the username and returns if it is available or not. here is an example:

 

<?php
  $user = $_GET['user'];
  $sql = sprintf("SELECT COUNT(*) FROM `users` WHERE `username` = '%s'",mysql_real_escape_string($user));
  list($test) = mysql_fetch_array(mysql_query($sql));
  print $test ? 1 : 0;
?>

 

Then, for the Ajax, just call that script with the value of the username as a GET variable. do you need help with that part too?

 

the second part is the ajax that calls the script.

Link to comment
Share on other sites

Using jQuery, the AJAX would look like:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
  function check_username ( ) {
    document.getElementById('user_status').innerHTML = '';
    $.get("check_user.php", { user: document.getElementById('username').value },
      function(data){
        document.getElementById('user_status').innerHTML = parseInt(data)
          ? 'Username Available'
          : 'Username Not Available';
      }
    );
  }
</script>
<input type="text" id="username" name="username" />
<input type="button" value="Check Availability" onclick="check_username()" />
<span id="user_status"></span>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.