Jump to content

Check username


smproph

Recommended Posts

I am trying to make a text field when you go to it and start typing in a clients name it checks the database to see if that name is in there and if it is not tell the user it is a new record. If it is already in there not to say anything. Well here is the code below. I keep getting that it is a new record even when i type specifically a name that is already in the database. Thanks

<?
//connect to database  

mysql_connect('localhost', '**', '**');  
mysql_select_db('**');  
  
//get the username  
$course = mysql_real_escape_string($_POST['course']);  
  
//mysql query to select field username if it's equal to the username that we check '    
$result = mysql_query('select Identifier from timeslip where Identifier = "'. $course .'"');  
  
//if number of rows fields is bigger them 0 that means it's NOT available '  
if(mysql_num_rows($result)>0){  
    //and we send 0 to the ajax request  
    echo 0;  
}else{  
    //else if it's not bigger then 0, then it's available '  
    //and we send 1 to the ajax request  
    echo 1;  
}  
?>

$(document).ready(function() {  
  
        //the min chars for username  
        var min_chars = 3;  
  
        //result texts  
        var characters_error = 'Minimum amount of chars is 3';  
        var checking_html = 'Checking...';  
  
        //when button is clicked  
       $('#course').keyup(function(){ 

            //run the character number check  
            if($('#course').val().length < min_chars){  
                //if it's bellow the minimum show characters_error text '  
                $('#username_availability_result').html(characters_error);  
            }else{  
                //else show the cheking_text and run the function to check  
                $('#username_availability_result').html(checking_html);  
                check_availability();  
            }  
        });  
  
  });  
  
//function to check username availability  
function check_availability(){  
  
        //get the username  
        var username = $('#course').val();  
  
        //use ajax to run the check  
        $.post("../checkid.php", { username: username },  
            function(result){  
                //if the result is 1  
                if(result == 1){  
                    //show that the username is available  
                    $('#username_availability_result').html(username + ' is a new record');  
                }
			else{  
                    //show that the username is NOT available  
                    $('#username_availability_result').html(username + ' is a current record');
                } 
			  
        });  
  
}  

<div id='username_availability_result'></div>  
            Last, First<label></label>
		<input placeholder="Smith, John" type="text" name="course" id="course" />

 

Link to comment
https://forums.phpfreaks.com/topic/222524-check-username/
Share on other sites

When you echo out the SQL, what do you get?

<?php
$sql = 'select Identifier from timeslip where Identifier = "'. $course .'"';
echo $sql;

 

And when you paste that query into your MySQL editor, what do you get back? Are there any errors? If there are, what are the errors?

Link to comment
https://forums.phpfreaks.com/topic/222524-check-username/#findComment-1150869
Share on other sites

When I ran it into sql it brings back all the Indenitifers.

 

I changed the code to this. I hard coded the usernames in there to make sure it was checking against them and now everytime I get that "This is a NEW Client" no matter if I put the correct name or not in there.


$existing_users=array('roshan','mike','jason');
//value got from the get metho
$user_name=$_REQUEST['course'];
//checking weather user exists or not in $existing_users array
if (in_array($user_name, $existing_users))
{
//user name is not available
echo "no";
}
else
{
  //username available i.e. user name doesn't exists in array
  echo "yes";
}
?>

$(document).ready(function()
{
$("#course").blur(function()
{
	//remove all the class add the messagebox classes and start fading
	$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
	//check the username exists or not from ajax
	$.post("../user_availability.php",{ user_name:$(this).val() } ,function(data)
        {
	  if(data=='no') //if username not avaiable
	  {
	  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
		{ 
		  //add message and change the class of the box and start fading
		  $(this).html('Existing Client').addClass('messageboxok').fadeTo(900,1);
		});		
          }
	  else
	  {
	  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
		{ 
		  //add message and change the class of the box and start fading
		  $(this).html('This is a NEW Client').addClass('messageboxerror').fadeTo(900,1);	
		});
	  }

        });

});
});

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/222524-check-username/#findComment-1150915
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.