James Stone
asked on
using arrows and tab to select from auto suggest list
I have a form with several text boxes that need to be populated with existing data from a db. I found a script that uses jquery/css/php to "auto suggest". I have managed to customize it enough that each box is drawing from a different query.
Now I would like to make it where I can use keyboard to arrow down the list and then select an item (fill my input box) by using the tab.
Not sure what Yahoo search assist uses but it works the way I would like mine to work.
I am not tied to this script, but I know I can make as many input boxes as needed and get the list of hints with it.
Thanks for any help.
Now I would like to make it where I can use keyboard to arrow down the list and then select an item (fill my input box) by using the tab.
Not sure what Yahoo search assist uses but it works the way I would like mine to work.
I am not tied to this script, but I know I can make as many input boxes as needed and get the list of hints with it.
Thanks for any help.
///////////////////////// rpc.php ///////////////////////////////
<?php
// echo$_POST['queryString'];
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'root' ,'******', 'webtracks');
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$origString = $db->real_escape_string($_POST['queryString']);
$partsString = explode(":",$origString);
$queryString =$partsString[0];
$loc=$partsString[1];
// Is the string length greater than 0?
if(strlen($queryString) >0) {
switch ($loc){ //different loc need different queries
case 1: // coordinator
$qry="select * from client where ClientName LIKE '%$queryString%' LIMIT 10";
break;
case 2:
$qry="select *,concat(firstname, ' ',lastname) as 'FullName' from coordinators where concat_ws(' ',firstname,lastname) LIKE '%$queryString%' LIMIT 10";
break;
case 3:
//echo "3<p>";
$qry="select * from stockid where StockName LIKE '%$queryString%' LIMIT 10";
break;
case 4:
//echo "4<p>";
$qry="select * from location where LocationName LIKE '%$queryString%' LIMIT 10";
break;
}
$query =$db->query($qry);
// end of switch to make query
if($query) {
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
while ($result = $query ->fetch_object()) {
// Format the results, im using <li> for the list, you can change it.
// The onClick function fills the textbox with the result.
switch ($loc){ // different loc need different results
case 1: // coordinator
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill1(\''.$result->ClientId.'-'.$result->ClientName.'\');">'.$result->ClientId.'-'.$result->ClientName.'</li>';
break;
case 2: // coordinator
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill2(\''.$result->SalesCoordinatorId.'-'.$result->FullName.'\');">'.$result->SalesCoordinatorId.'-'.$result->FullName.'</li>';
break;
case 3:
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill3(\''.$result->StockName.'\');">'.$result->StockName.'-'.$result->Description.'</li>';
break;
case 4:
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill4(\''.$result->LocationName.'\');">'.$result->LocationName.'-'.$result->LocationDescription.'</li>';
break;
}// end of switch to make result
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {// Dont do anything
}// There is a queryString.
} else {
echo "There should be no direct access to this script!";
}
}
?>
////////////////////////////// form ////////////////////////////////////////
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Auto Suggest</title>
<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script type="text/javascript" src="lookup_function.js"></script>
<style type="text/css">
body {
font-family: Helvetica;
font-size: 11px;
color: #000;
}
h3 {
margin: 0px;
padding: 0px;
}
.suggestionsBox {
position: relative;
left: 30px;
margin: 10px 0px 0px 0px;
width: 200px;
background-color: #212427;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 2px solid #000;
color: #fff;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}
.suggestionList li:hover {
background-color: #659CD8;
}
</style>
</head>
<body>
<div>
<form>
<div>
Clients:
<br />
<input type="text" size="30" value="" id="inputString1" onkeyup="lookup(this.value,1);" onblur="fill1();" />
</div>
<div class="suggestionsBox" id="suggestions1" style="display: none;">
<img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="autoSuggestionsList1">
</div>
</div>
<div>
Coordinators:
<br />
<input type="text" size="30" value="" id="inputString2" onkeyup="lookup(this.value,2);" onblur="fill2();" />
</div>
<div class="suggestionsBox" id="suggestions2" style="display: none;">
<img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="autoSuggestionsList2">
</div>
</div>
<div>
Stock Id:
<br />
<input type="text" size="30" value="" id="inputString3" onkeyup="lookup(this.value,3);" onblur="fill3();" />
</div>
<div class="suggestionsBox" id="suggestions3" style="display: none;">
<img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="autoSuggestionsList3">
</div>
</div>
<div>
Location:
<br />
<input type="text" size="30" value="" id="inputString4" onkeyup="lookup(this.value,4);" onblur="fill4();" />
</div>
<div class="suggestionsBox" id="suggestions4" style="display: none;">
<img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="autoSuggestionsList4">
</div>
</div>
</form>
</div>
</body>
</html>
/////////////// lookup_function.js ///////////////////////////////
function lookup(inputString,loc) {
var css = '#suggestions'+loc;
var list = '#autoSuggestionsList'+loc; // loc determines which box to populate // sent here then sent back by php
if(inputString.length == 0) {
// Hide the suggestion box.
$(css).hide();
} else {
inputString=inputString+":"+loc;
//alert(list+"-"+inputString);
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$(css).show();
$(list).html(data);
}
});
}
} // lookup
function fill1(thisValue) { // clients
$('#inputString1').val(thisValue);
setTimeout("$('#suggestions1').hide();", 200);
}
function fill2(thisValue) { //coordinators
$('#inputString2').val(thisValue);
setTimeout("$('#suggestions2').hide();", 200);
}
function fill3(thisValue) { //Stock Id
$('#inputString3').val(thisValue);
setTimeout("$('#suggestions3').hide();", 200);
}
function fill4(thisValue) { //Location
$('#inputString4').val(thisValue);
setTimeout("$('#suggestions4').hide();", 200);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I found exactly what I wanted.