0
What is the logic behind live autocomplete search ?
Autocomplete search box which we use to search data from mysql database.
2 odpowiedzi
+ 2
short answere ajax
long anwere a javascript function
that retrievs new data from the server every time you add a char to the searchbar
once you know how ajax work you'll recogrice it everywhere
0
https://code.sololearn.com/W1lLsDzlkHQR/?ref=app
This one searches a local json object. The bit inside the q.length > 0 check is what would change to an ajax call. If you use the jQuery library that would be:
$.ajax({
type: "get",
url: "path/to/php/file.php?var1=val1&var2=val2",
success: function(response) {
var data = JSON.parse(response);
// data is now a JSON object of your db return. process is how you want
}
});
in your php file:
// example assumes mysqli is used alter as you need
$query = "query here";
$result = $mysql->query($query);
$return = array();
while($row = $result->fetch_assoc()) {
// any data pre-processing you want to do
$return[] = $row;
}
echo json_encode($return, JSON_HEX_APOS);
this will get your data and build an 2 dimensional array which will get sent back to the javascript as a JSON string.