I am basically trying to create a small registration form. If the username is already taken, I want to add the 'red' class, if not then 'green'.
The PHP here works fine, and returns either a "YES" or "NO" to determine whether it's ok.
The CSS:
input {
border:1px solid #ccc;
}
.red {
border:1px solid #c00;
}
.green {
border:1px solid green;
background:#afdfaf;
}
The Javascript I'm using is:
$("#username").change(function() {
var value = $("#username").val();
if(value!= '') {
$.post("check.php", {
value: value
}, function(data){
$("#test").html(data);
if(data=='YES') {
$("#username").removeClass('red').addClass('green');
} if(data=='NO') {
$("#username").removeClass('green').addClass('red');
}
});
}
});
I've got the document.ready stuff too... It all works fine because the #test div html changes to either "YES" or "NO", apart from the last part where I check what the value of the data is. Here is the php:
$value=$_POST['value'];
if ($value!="") {
$sql="select * FROM users WHERE username='".$value."'";
$query = mysql_query($sql) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($query);
if ($num_rows > 0) {
echo "NO";
} else {
echo "YES";
}
}