This is from a register form. This function verifies if the username is available or not.
jQuery:
/* Verifies if the username is available */
$.post('functions/usernameDisponibility.php',{usernamePHP:username},function(disponibility)
{
$('.usernameErrors').text(disponibility);
if(disponibility==true)
{
$('#username').css('border-color','#00ff00');
}else
{
$('#username').css('border-color','red');
$('.usernameErrors').text('Username unavailable');
}
});
PHP:
if(isset($_POST['usernamePHP'])&&!empty($_POST['usernamePHP']))
{
$username = $_POST['usernamePHP'];
$Connect = mysql_connect('localhost','root','');
mysql_select_db('phplogin');
$query= mysql_query("SELECT * FROM users WHERE username = '$username'");
$result= mysql_num_rows($query);
if($result==1)
{
echo false;
}else
{
echo true;
}
}
The problem is in the error display. When disponibility != true, it shows “Username unavailable”, but when it’s true it shows “1”. My guess is that “1” came from the PHP script but even if I do this:
if(disponibility==true)
{
$('#username').css('border-color','#00ff00');
$('.usernameErrors').text('');
}else
The “1” still shows up very quickly and only then it is replace by text(”). What can I do for the “1” not be displayed at all?
it’s because of this:
you are outputing the content of “disponibility” variable before you do any checks. Remove that line