Trying to get my live search to work ‘properly’. When I enter an ‘a’ I get ‘aussie’ and ‘australian’ (not sure where these are coming from as they are not in my db) in a area below the box (like a drop down). I can select one of them and it populates the text box. The code suggested to me uses mysqli which I am not familiar with. When I add a typical SELECT statement I am use to I see the correct data (more or less) but it is not selectable. The reason I say more or less is an ‘a’ gives me ‘aussie’ and ‘australian’ like before (in a drop down). Adding a ‘n’ (i.e. searching for ‘an’) gives me list of all names in my database that contain the string in a DIV but not selectable.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#faq_search_input").keyup(function() {
var faq_search_input = $(this).val();
var dataString = 'keyword='+ faq_search_input;
if(faq_search_input.length>1) {
$.ajax({
type: "GET",
url: "ajax-search.php",
data: dataString,
success: function(server_response) {
document.getElementById("searchresultdata").style.display = "block";
$('#searchresultdata').html(server_response).show();
}
});
} return false;
});
});
</script>
</head>
<body>
<div class="searchholder">
<input name="query" class="quicksearch" type="text" id="faq_search_input" />
<div id="searchresultdata" class="searchresults" style="display:none;"> </div>
</div>
</body>
</html>
ajax-search.php
<?php
//you must define your database settings
define("DB_HOST", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "");
define("DB_NAME", "mydb");
if(isset($_GET['keyword'])) {
$search = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($search->connect_errno) {
echo "Failed to connect to MySQL: (" . $search->connect_errno . ") " . $search->connect_error;
$search->close();
}
$keyword = trim($_GET['keyword']) ;
//original statement
//$query ="SELECT COLUMN_NAME FROM ".DB_NAME.".INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%".$keyword."%'";
//my edited statement
//$query ="SELECT name FROM ".DB_NAME.".INFORMATION_SCHEMA.COLUMNS WHERE name LIKE '%".$keyword."%'";
//basic sql statement
$query ="SELECT name FROM users WHERE name LIKE '%".$keyword."%'";
$values = $search->query($query);
if( $search->error ) exit( $search->error );
if($values->num_rows != 0) {
while($row = $values->fetch_assoc()) {
echo $row['name']."<br>";
}
}
else {
echo 'No Results for :"'.$_GET['keyword'].'"';
}
}
?>
database contains a table of user data called users with a column called name.
Any thoughts?
Used solution found in the tutorial here
http://www.htmlblog.us/jquery-autocomplete