我一直在尝试使用php ajax mysql实现实时搜索。我能够在击键时拉出结果,但我不知道如何启用选择任何结果。
有人可以帮我知道怎么做吗?
以下是ref的代码: HTML:
<div class="searchBar" >
<input type="text" id="searchResidents" name="searchResidents" placeholder="Enter a keyword here..." onkeyup=" showResult(this.value)">
<input type="submit" id="searchSubmit" name="searchSubmit" value="search">
</div>
JS:
function showResult(str){
onsole.log("Livesearch js is invoked");
if (str.length==0)
{
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
PHP:
for($i=0;$i<$arraySize;$i++)
{
if($stmt = $mysqli->prepare($query)){
$param="%".$userInput[$i]."%";
$ids=array();
$stmt->bind_param('sss', $param,$param,$param);
$stmt->execute();
$stmt->bind_result($id, $name,$company, $title, $bio, $website, $imgURL);
if($stmt->fetch()){
if(!array_search($id,$ids))
{
$ids[$i]=$id;
$findResult=true;
//If there is a result, display the results
echo '<span class="peoplesection">
<a href="javascript:void(0);" class="viewOverlay" rel='.$id.'>
<img src='.$imgURL.' alt="Photo of: "'.$name.'>
<h1 class="peopleh1">'.$name.'</h1>
<h2 class="peopleh2">'.$company.'</h2>
<h3 class="peopleh3">'.$title.'</h3>
</a>
</span>';
}
}
else
{
$stmt->close();
}
}
}
感谢。