<html>
<head>
<script>
function getHint(str) {
if (str.length == 0) {
document.getElementById(“txtHint”).innerHTML = “”;
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById(“txtHint”).innerHTML = this.responseText;
}
};
xmlhttp.open(“GET”, “findvalue.php?q=” + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>

<p><b>Enter name in input field below:</b></p>
<form>
Name: <input type=”text” onkeyup=”getHint(this.value)”>
</form>
<p>Suggestions: <span id=”txtHint”></span></p>
</body>
</html>

File findvalue.php

<?php
// Array with names
$a[] = “Ann”;
$a[] = “Britt”;
$a[] = “Cinella”;
$a[] = “Eva”;
$a[] = “Fia”;
$a[] = “Gnda”;
$a[] = “Hee”;
$a[] = “In”;
$a[] = “Oph”;
$a[] = “unia”;
$a[] = “Ama”;
$a[] = “Raquel”;
$a[] = “Cidy”;
$a[] = “Dis”;
$a[] = “Eve”;
$a[] = “Evta”;
$a[] = “Sunva”;
$a[] = “Wehe”;
$a[] = “Viky”;

$q = $_REQUEST[“q”];
$hint = “”;
if ($q !== “”) {
$q = strtolower($q);
$len=strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === “”) {
$hint = $name;
} else {
$hint .= “, $name”;
}
}
}
}

echo $hint === “” ? “no suggestion” : $hint;
?>

Please login or Register to submit your answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.