In response to pradeep's Initial Code:
You must switch the following code snippets in your PHP file:
if(mysql_num_rows($result) > 0)
This is because you WANT PHP to call out an error if you have one or more results because this means you have that user name taken already in the database. If you don't have any results, you may create that username successfully, thus echo "+OK";
So this condition is met now:
if(r.indexOf("+OK") == 0)
the form is then submitted, otherwise you get the alert code snippet.
Tested my code to make sure.