Lots of places to look at with lots of idea how to do this but I'm not succeeding with any of them.
I have an input field called 'callsign' as you type it goes out to a MySQL table and returns suggestions based on what you have already typed. I want hints as to what the value should be to appear in the input field so I can select one of them or just keep typing. When I select the one I want I also want the Fname field to fill in, it is also returned from MySQL, see the XML below.
I really want to learn how to do this but a long explanation using terms I only sort of understand won't go nearly as far as an example. Help!
The input looks like this:
Call Sign: <input type='text' value='' placeholder='Search' id='cs1' style='text-transform:uppercase' autofocus onkeyup='showHint(this.value)'> Name: <Input type='text' name='Fname'style='text-transform:capitalize'>
The MySQL like this:
<?php
require_once "dbConnectDtls.php";
$q = $_REQUEST["q"];
$stmt = $db_found->prepare("SELECT DISTINCT callsign, Fname
FROM NetLog
WHERE recordID IN (SELECT max(recordID)
FROM NetLog
WHERE callsign LIKE ? GROUP BY callsign)");
$stmt->execute(array("$q%"));
$result = $stmt->fetchAll();
print_r($result);
?>
The result of running the test MySQL above with a q = 'w'
returns:
Array ( [0] => Array ( [callsign] => W0DLK [0] => W0DLK [Fname] => Deb [1]
=> Deb ) [1] => Array ( [callsign] => W0GPS [0] => W0GPS [Fname] =>
John [1] => John ) [2] => Array ( [callsign] => WA0TJT [0] => WA0TJT
[Fname] => Keith [1] => Keith ) [3] => Array ( [callsign] => W0KCN [0]
=> W0KCN [Fname] => Net Control [1] => Net Control ) )
And my function like this:
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "gethint.php?q=" + str, true);
xmlhttp.send();
}
}