I am trying to get a form to check if a surname already exists in the database.
It seems like it is working, it checks the database and returns values, eg the number, as a string, of times the surname exists, or 'none' if they did not enter a surname.
The problem I am having is when I try to display the results.
JavaScript:
sName = $('#sName'), sInfo = $("#sInfo");
sName.blur(function() {
$.ajax({
type: "POST",
data: "sName="+$(this).attr("value"),
url: "check.php",
beforeSend: function(){
sInfo.html("<font color='blue'>Checking Surname...</font>");
},
success: function(data){
if(data == "none")
{
sInfo.html("<font color='red'>No Surname Entered</font>");
}
else if(data != "0")
{
sInfo.html("<font color='red'>Surname Exists</font>");
}
else
{
sInfo.html("<font color='green'>Surname Not Found</font>");
}
}
});
});
check.php:
//database connection stuff
extract($_REQUEST);
if(!empty($sName))
{
$sql = "SELECT sName FROM student WHERE sName='$sName'";
$rsd = mysqli_query($DBConnect, $sql) or die('Error: '.mysqli_error($DBConnect));
$msg = mysqli_num_rows($rsd);
mysqli_free_result($rsd);
mysqli_close($DBConnect);
}
else
{
$msg = "none";
}
echo $msg;
No matter what the value of data
is (eg "1", "2", "0", "none") the sInfo
div always displays "Surname Exists".
When I change it to display
else if(data != "0")
{
sInfo.html("<font color='red'>Surname Exists..."+data+"</font>");
}
The result is Surname Exists...0 or Surname Exists...none when it should instead display No Surname Entered or Surname Not Found...
What am I doing wrong?
thanks.