Question

i created a simple search engine that displays mysql database results using the php "LIKE" function (code below). everything works fine. i would just like to make it so that when the user starts typing he/she can use the arrow keys to scroll down and press enter on an item just like google. thanks. my code:

HTML:

<input type="text" name='search' id="searchbooks" onkeyup='getbooks(this.value);'       value="search" onblur="setTimeout('removedrop()', 80);">

<div id='drop'></div>   

JAVASCRIPT:

function getbooks(value){
    if (value!=""){



    $.post('getbooks.php', {book: value},
        function (data) {
            $('#drop').html(data);
            doCSS();                
        });

        }
        else {

        $('#drop').html("");
        undoCSS();
        }

}
getbooks.php file:

<?php
include 'connect.php';

$book=mysql_real_escape_string(addslashes($_POST['book']));

$result=mysql_query("SELECT * FROM searchengine WHERE title LIKE '$book%'");


while ($row=mysql_fetch_assoc($result)){
$title=$row['title'];
$id=$row['id'];


echo "<div id='link'><a href='index.php?id=$id' id='words'>". $row['title'] ."</a></div>";

}
?>
Was it helpful?

Solution

How about using the jQuery autocomplete plugin? It's made for exactly this use case.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top