
by
Jeremy Canfield | Updated January 1st, 2017
On the page which has the search engine, ensure onkeyup="autocomplet()" is in the input form.
<form>
<input type="text" is="search_id" onkeyup="autocomplet()">
</form>
This is the JavaScript.
function autocomplet() {
var min_length = 0; // min caracters to display the autocomplete
var keyword = $('#search_id').val();
if (keyword.length >= min_length) {
$.ajax({
url: 'ajax_search_processing.php',
type: 'POST',
data: {keyword:keyword},
success:function(data){
$('#search_results').show();
$('#search_results').html(data);
}
});
} else {
$('#search_results').hide();
}
}
function set_item(item) {
// change input value
$('#search_id').val(item);
// hide proposition list
$('#search_results').hide();
}
This is the SQL stuff.
<?php
$con = new mysqli('ip_domain','username','password','database');
$keyword = $POST['keyword'];
$sql = "select * from tablename where name like '%$keyword%' ";
$sql_query = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($sql_query)) {
echo $row['data'];
}
?>
<div id="search_results"></div>