Jquery Autocomplete after space press

Posted by Limpep on Stack Overflow See other posts from Stack Overflow or by Limpep
Published on 2012-04-15T17:25:27Z Indexed on 2012/04/15 17:28 UTC
Read the original article Hit count: 188

Filed under:
|
|
|

I am having an issue with my auto-complete feature such as when a user presses the space button the auto-complete doesn't show up again. Here is my code

script type="text/javascript">
function lookup(inputString) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        $.post("autocomplete.php", { queryString: ""+inputString+"" }, function(data){

            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
        });
    }
} // lookup

function fill(thisValue) {

    $('#tag').val(thisValue);


     setTimeout("$('#suggestions').hide();", 200);

}

here my php code

<?php
require_once('config.php');

$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD,DB_DATABASE);

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'])) {
        $queryString = $db->real_escape_string($_POST['queryString']);

        // Is the string length greater than 0?

        if(strlen($queryString) >0) {
            // Run the query: We use LIKE '$queryString%'
            // The percentage sign is a wild-card, in my example of countries it works like this...
            // $queryString = 'Uni';
            // Returned data = 'United States, United Kindom';



            $query = $db->query("SELECT name FROM tag WHERE name LIKE '$queryString%' ORDER BY name LIMIT 10");
            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.


                    echo '<li onClick="fill(\''.$result->name.'\');">'.$result->name.'</li>';
                }
            } 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!';
    }
}

?>

Any help would be great, thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery