The demo below is the instant search function I build, just like google search, printing out result right after the user's input.
When testing, it runs smoothly on localhost, but on the web, it runs lagging, that is, when user typing in multiple characters fast, the keypress will not show in input box for some seconds to wait for data processing (I guess) then the charactered typed showed many seconds after . Absolutely this is no good for user's experience. How may I resolve this?
Frontend
<input onkeyup="searchq();" type="text">
<script>
function searchq(){
// get the value
txt = $("input").val();
// post the value
if(txt){
$.post("search.php", {searchVal: txt}, function(result){
$("#search_output").html(result+"<br><a class='anchor_tag_s' href='createobject.php?object="+txt+"'><div id='notfound'>Not found above? Create Here.</div><a>");
});
}
else{
$("#search_output").html("");
}
};
</script>
Backend
//query the search using match
$query=mysqli_query($conn,"SELECT * from objects WHERE Match(name) Against ('%$search%' in natural language mode) LIMIT 9") or die("could not search! Oops, Panpan might be hacked");
$count=mysqli_num_rows($query);
//if match no result using like query
if($count==0){
$query=mysqli_query($conn,"SELECT * from objects WHERE name LIKE '%$search%' LIMIT 9") or die("could not search! Oops, Panpan database might be hacked");
$count=mysqli_num_rows($query);
if ($count==0){
while($count==0){
//if matach and like show no result, remove the frist word of array, then search, if no result, do it again, if not, do it again.
$search = explode(" ",$search);
$search=array_slice($search,1,2);
$search=implode(" ",$search);
$query=mysqli_query($conn,"SELECT * from objects WHERE name LIKE '%$search%' LIMIT 9") or die("could not search! Oops, Panpan database might be hacked");
$count=mysqli_num_rows($query);
while($row=mysqli_fetch_assoc($query)){
$object_id=$row["object_id"];
$object_name=$row["name"];
$object_description=$row["description"];
$object_up=$row["up"];
$object_down=$row["down"];
$object_views=$row["views"];
$output=$object_name;
$result= "<a class='anchor_tag_s' href='"."object.php?id=".$object_id."'><div>".$output."</div></a>";
echo $result;
}
}
}
else{
// print out the like query
while($row=mysqli_fetch_assoc($query)){
$object_id=$row["object_id"];
$object_name=$row["name"];
$object_description=$row["description"];
$object_up=$row["up"];
$object_down=$row["down"];
$object_views=$row["views"];
$output=$object_name;
$result= "<a class='anchor_tag_s' href='"."object.php?id=".$object_id."'><div>".$output."</div></a>";
echo $result;
}
}
}
else{
// print out the match query
while($row=mysqli_fetch_assoc($query)){
$object_id=$row["object_id"];
$object_name=$row["name"];
$object_description=$row["description"];
$object_up=$row["up"];
$object_down=$row["down"];
$object_views=$row["views"];
$output=$object_name;
$result= "<a class='anchor_tag_s' href='"."object.php?id=".$object_id."'><div>".$output."</div></a>";
echo $result;
}
}
}
?>