What is the logic behind live autocomplete search ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the logic behind live autocomplete search ?

Autocomplete search box which we use to search data from mysql database.

17th May 2018, 2:28 AM
Manit Singh
Manit Singh - avatar
2 Answers
+ 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
17th May 2018, 3:52 AM
Lexfuturorum
Lexfuturorum - avatar
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.
17th May 2018, 3:49 AM
Adam
Adam - avatar