I am trying to check whether a name that the user is entering is valid or not. Very simple question but yes I am facing this problem.
I am writing following get the HTML and AJAX done:
<script>
function getUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
Name: <input type = "text" name="n1" onchange="getUser(this.value)">
<p id = "txtHint">
</form>
<br>
</body>
</html>
For PHP, it follows:
<?php
$q = $_REQUEST['q'];
#echo "$q";
$dc = mysql_connect('localhost','root','') or die(mysql_error());
if($dc > 0)
mysql_select_db("sayak");
$r = "SELECT * FROM `check` WHERE name = '$q' " or die(mysql_error());
#echo "$r";
$s = mysql_query($r) or die(mysql_error());
#echo "$s";
if($s > 0)
echo "Correct";
else
echo "Invalid";
mysql_close();
?>
In the case of names, that are not there in the database it is showing "Correct".
Thanks in advance.