Username availabiltiy

Hai Guys,

i have one case registration page in my project…in that page i check the case id is already registered or not…(using username availability code)…

That code is Work…But the case id is available it goes to next textbox(error msg is “Please select another case id” )…when he select another case id then only it goes to next textbox otherwise dont go to next textbox…

newcase.php:

<SCRIPT type=“text/javascript”>
<!–
/*
Credits: Bit Repository
Source: http://www.bitrepository.com/web-programming/ajax/username-checker.html
*/

pic1 = new Image(16, 16);
pic1.src = “loader.gif”;

$(document).ready(function(){

$(“#case_id”).change(function() {

var usr = $(“#case_id”).val();

if(usr.length >= 2)
{
$(“#status”).html(‘<img src=“username_checker/loader.gif” align=“absmiddle”> Checking availability…’);

$.ajax({  
type: "POST",  
url: "username_checker/check1.php",  
data: "case_id="+ usr,  
success: function(msg){  

$(“#status”).ajaxComplete(function(event, request, settings){

if(msg == 'OK')
{ 
    $("#case_id").removeClass('object_error'); // if necessary
	$("#case_id").addClass("object_ok");
	$(this).html('&nbsp;&lt;img src="username_checker/tick.gif" align="absmiddle"&gt;');
}  
else  
{  
	$("#case_id").removeClass('object_ok'); // if necessary
	$("#case_id").addClass("object_error");
	$(this).html(msg);
}  

});

}

});

}
else
{
/*
$(“#status”).html(‘<font color=“red”>The username should have at least <strong>4</strong> characters.</font>’);
$(“#case_id”).removeClass(‘object_ok’); // if necessary
$(“#case_id”).addClass(“object_error”);
*/}

});

});

//–>
</SCRIPT>
<form action=“” name=“forfm1” id=“forfm1” method=“post” enctype=“multipart/form-data” onsubmit=“return checkForm1(forfm1);”>
<table width=“100%” border=“0” class=“gridtable”>

            &lt;tbody&gt;
             &lt;tr&gt;
                &lt;td width="50%" class="header"&gt;Case Id :&lt;/td&gt;                
                &lt;td width="50%"&gt;
           
                  &lt;input type="text" name="case_id" id="case_id" value="&lt;?php if($_GET['action']=="edit") echo $data[0]['case_id'];?&gt;" /&gt;
                  &lt;div id="status"&gt;&lt;/div&gt;
                                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                &lt;td width="50%" class="header"&gt;Case Name :&lt;/td&gt;                
                &lt;td width="50%"&gt;
                  &lt;input type="text" name="dms_case_name" id="dms_case_name" value="&lt;?php if($_GET['action']=="edit") echo $data[0]['dms_case_name'];?&gt;"/&gt;
                                &lt;/td&gt;
              &lt;/tr&gt;
              
              &lt;tr&gt;
                &lt;td class="header"&gt;Client First Name :&lt;/td&gt;
                &lt;td&gt;&lt;input type="text" name="dms_client_fname" id="dms_client_fname" value="&lt;?php if($_GET['action']=="edit") echo $data[0]['dms_client_fname'];?&gt;"/&gt;&lt;/td&gt;
              &lt;/tr&gt;

</form>

<?php

if(isset($_POST[‘case_id’]))
{
$case_id = $_POST[‘case_id’];

$dbHost = ‘localhost’; // usually localhost
$dbUsername = ‘root’;
$dbPassword = ‘’;
$dbDatabase = ‘jdms’;

$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die (“Unable to connect to Database Server.”);
mysql_select_db ($dbDatabase, $db) or die (“Could not select database.”);

$sql_check = mysql_query(“select id from dms_case where case_id='”.$case_id.“'”) or die(mysql_error());

if(mysql_num_rows($sql_check))
{

echo ‘<font color=“red”>The Case Id <STRONG>’.$case_id.‘</STRONG> is already in use.</font>’;
}
else
{
echo ‘OK’;
}

}

?>

Are you asking how to best avoid having duplicate usernames in a registration system?

If so, one practice is to try and insert the new username, if the username column in Mysql has a UNIQUE index on it, then Mysql returns a recognised error number(1062), which you can catch and then use to prompt for a new username.

Try this for starters [google]mysql unique usernames[/google]