Jump to content

Username avaliability checker keeps returning 'taken'.


Russia

Recommended Posts

Hey guys, I have this little script, but it doesn't seem to work properly.

 

 

<?php
mysql_connect("localhost","root","");
mysql_select_db("chat");
$user = strip_tags(trim($_REQUEST['username']));
if(strlen($user) <= 0)
{
  echo json_encode(array('code'  =>  -1,'result'  => 'Invalid username, please try again.'));
  die;
}
// Query database to check if the username is available
$query = "Select * from users where username = '$user' ";

$result = mysql_query($query);
$avaliable = mysql_num_rows($result);
// Execute the above query using your own script and if it return you the
// result (row) we should return negative, else a success message.
//$available = true;
if($available)
{
  echo json_encode(array('code'  =>  1,'result'  =>  "Success,username $user is still available"));
  die;
}
else
{
  echo  json_encode(array('code'  =>  0,'result'  =>  "Sorry but username $user is already taken."));
  die;
}
die;
?>

 

It keeps returning with 'Sorry but username iunsdbsyusaddssd763b373b is already taken'.

 

Even when I type in some random gibberish like 'iunsdbsyusaddssd763b373b'

 

Can anyone show me why it is doing that?

 

Thanks alot friends.

 

Link to comment
Share on other sites

An example of the logic involved in executing a select query.

 

$sql = "SELECT fld FROM tbl";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    // it is safe to use $result.
  } else {
    // no records returned
  }
} else {
  // query failed, handle error.
}

Link to comment
Share on other sites

Okay, I changed it to this:

 

<?php
mysql_connect("localhost","root","");
mysql_select_db("chat");
$user = strip_tags(trim($_REQUEST['username']));
if(strlen($user) <= 0)
{
  echo json_encode(array('code'  =>  -1,'result'  => 'Invalid username, please try again.'));
  die;
}
// Query database to check if the username is available
$sql = "Select * from users where username = '$user' ";

//$result = mysql_query($query);
//$avaliable = mysql_num_rows($result);
// Execute the above query using your own script and if it return you the
// result (row) we should return negative, else a success message.
//$available = true;



if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
echo json_encode(array('code'  =>  1,'result'  =>  "Success,username $user is still available"));
die;
  } 
  else {
   
     echo  json_encode(array('code'  =>  0,'result'  =>  "Sorry but username $user is already taken."));
  die;
  }
} 


?>

 

It still returns as

{"code":1,"result":"Success,username admin is still available"}

 

What seems to be the problem?

Link to comment
Share on other sites

Thanks, also, one more thing, how would I do it so it enables the submit button once the message is Avaliable username.

 

I have this code:

 

<script type="text/javascript">
  //function to create ajax object
  function pullAjax(){
    var a;
    try{
      a=new XMLHttpRequest()
    }
    catch(b)
    {
      try
      {
        a=new ActiveXObject("Msxml2.XMLHTTP")
      }catch(b)
      {
        try
        {
          a=new ActiveXObject("Microsoft.XMLHTTP")
        }
        catch(b)
        {
          alert("Your browser broke!");return false
        }
      }
    }
    return a;
  }

  function validate()
  {
    site_root = '';
    var x = document.getElementById('uname');
    var msg = document.getElementById('msg');
var submituser = document.getElementById('submituser');
    user = x.value;

    code = '';
    message = '';
    obj=pullAjax();
    obj.onreadystatechange=function()
    {
      if(obj.readyState==4)
      {
        eval("result = "+obj.responseText);
        code = result['code'];
        message = result['result'];

        if(code <=0)
        {
          x.style.border = "1px solid red";
          msg.style.color = "red";

        }
        else
        {
          x.style.border = "1px solid #000";
          msg.style.color = "green";
	  usersubmit.style.display = 'block';
        }
        msg.innerHTML = message;
      }
    }
    obj.open("GET",site_root+"js/user_availability.php?username="+user,true);
    obj.send(null);
  }
</script>

 

and the form is here:

echo "<form action='' method='POST'>";
    echo '
User Name : 
<input type="text" onblur="validate();" id="uname" name="uname" value="" />
<div id="msg"></div>';

echo "next info";	 	

    echo "<input name='step3' value='Next' id='submituser' style='display:none' type='submit' />";
    echo "</form>";

 

I set it as display none as a default and then when the box is green for the box to show as a block.

 

Its not showing up tho after the box becomes green

Link to comment
Share on other sites

This is a completely different question and belongs in the Ajax board. I suggest you open a new thread there.

 

While your in the Ajax board you should read the very first sticky, no one writes there own Ajax methods these days.

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.