开发者

jQuery and PHP search suggestion script

开发者 https://www.devze.com 2023-03-03 02:03 出处:网络
I have a PHP and jQuery script that creates search result suggestions from a text box. However, when you type something in the text box, delete it and try making a different query, no suggestions are

I have a PHP and jQuery script that creates search result suggestions from a text box. However, when you type something in the text box, delete it and try making a different query, no suggestions are displayed. Why could this be?

Here is a copy of my webpage code:

<script type="text/JavaScript">
function lookup(inputString){
if (inputString.length==0){
$('#suggestions').hid开发者_Go百科e();
} else{
$.post("suggestions.php",{
queryString: "" + inputString + ""},
function(data){
$('#suggestions').html(data);
});
}
}
</script>

<form>
<input type="text" size="30"  onkeyup="lookup(this.value);">
<div id="suggestions"></div>
</form>

Here is a copy of my PHP code:

<p id="searchresults"><?php

$db=new mysqli('localhost','username','password','database');

if(isset($_POST['queryString'])){
$queryString=$db->real_escape_string($_POST['queryString']);
            if(strlen($queryString)>0){
                $query = $db->query("SELECT * FROM search s WHERE name LIKE '%" . $queryString . "%'");
                if($query){
                    while ($result = $query ->fetch_object()){
                        echo '<a href="'.$result->name.'">';                        
                        $name=$result->name;            
                        echo ''.$name.'';
                    }
                }
            }
        }
?></p>

Thanks in advance, Callum


You hide but don't show again.

change your callback function to:

function(data){
    $('#suggestions').html(data).show();
}

or a fadeIn() for added cuteness ;)


Add $('#suggestions').show() inside of your else statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号