Twitter Provides awesome typeahead javascript plugin to have google.com like search suggestion.
Note: Twitter Bootsrap Typeahead was Replaced with new Twitter typeahead. So Dont Confuse both.
For the Complete Working of the AutoSuggest Feature we need following files.
Latest version of this Tutorial is available here
JavaScript Files
CSS Files
Complete Code of Index.html
Need a PHP File connection.php to fetch json data from Database
Thats it.
Note: Twitter Bootsrap Typeahead was Replaced with new Twitter typeahead. So Dont Confuse both.
For the Complete Working of the AutoSuggest Feature we need following files.
Latest version of this Tutorial is available here
JavaScript Files
- JQuery.js (Latest)
- bootstrap.js or bootstrap.min.js
- typeahead.js or typeahead.min.js
CSS Files
- bootstrap.css or bootstrap.min.css
<script src="http://cdn.jsdelivr.net/typeahead.js/0.9.3/typeahead.min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
Complete Code of Index.html
<!DOCTYPE> <html lang="en"> <head> <title>Typeahead.js Tutorial with Mysql Database</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <meta name="generator" content="Geany 1.23.1" /> </head> <link href="bootstrap.min.css" rel="stylesheet" /> <script type="text/javascript" src="jquery-2.0.3.min.js"></script> <script type="text/javascript" src="bootstrap.min.js"></script> <script type="text/javascript" src="typeahead.min.js"></script> <style> .tt-query, .tt-hint { width: 396px; height: 30px; padding: 8px 12px; font-size: 24px; line-height: 30px; border: 2px solid #ccc; -webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px; outline: none; } .tt-query { -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); } .tt-hint { color: #999 } .tt-dropdown-menu { width: 422px; margin-top: 12px; padding: 8px 0; background-color: #fff; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.2); -webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px; -webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2); -moz-box-shadow: 0 5px 10px rgba(0,0,0,.2); box-shadow: 0 5px 10px rgba(0,0,0,.2); } .tt-suggestion { padding: 3px 20px; font-size: 18px; line-height: 24px; } .tt-suggestion.tt-is-under-cursor { color: #fff; background-color: #0097cf; } </style> <script> $(document).ready(function(){ $("#search").typeahead({ name : 'sear', remote: { url : 'connection.php?query=%QUERY' } }); }); </script> <body> <input type="text" name="search" id="search"> </body> </html>
Need a PHP File connection.php to fetch json data from Database
<?php $con = new mysqli("localhost","root","","autopin"); $result = $con->query("SELECT id,imageurl FROM pinterest LIMIT 0,10"); while ($row = $result->fetch_object()){ $user_arr[] = $row->id; $user_arr2[] = $row->imageurl; } $result->close(); echo json_encode($user_arr2); ?>
Thats it.
Tags:
jQuery
I am Using the FOllowing Code
ReplyDeletename : 'search',
valueKey: '',
limit:10,
remote: {
url : base_url/'
},
My Bootstrap is Showing only one Auto Suggestion. But My Json Response is an Array of Object.
Guide me.
Twitter Bootstrap Need to Set the valueKey Property.
DeleteIts Emty in Your Case.
Set valueKey:"product_id" // Or What ever.
hi,
ReplyDeleteI want to get that $user_arr[] values what we have to do
thanks and advance
echo json_encode($user_arr);
DeleteI copied the codes as is and it worked fine but I noticed, only up to 10 records retrieved from database even LIMIT 0,10 is removed. I want to retrieve the whole list of names from the database.
ReplyDeleteAny help is much appreciated.
That's the default value.
DeleteYou should use a combination of limit and minLength. limit is the max number of suggestions that will be displayed for a given query (defaults to 5) and minLength is the minimum number of characters a query needs before suggestions start to get rendered
That's really poorly coded.
ReplyDeleteYes Dear friend, Its Demonstration. You can split the CSS and JS to Separate Files.
DeleteThanks for the simple and useful code.
ReplyDeleteThe selector for jquery document ready is wrongly done.
ReplyDeleteSupposed to be
$(document).ready
and NOT
$("document").ready as bove.
Thanks so much for this tutorial man
hey my friend... i am trying to get the my ID together but i don't know how to do... could you explain a little better?
ReplyDeleteexample:
I have
ID - Col1 - Col2
It's only showing the Col1... I need the ID too...
Thank you
i have array object how to use this for type ahead can you give any example with complete code with proper using type head js thanks in advance
ReplyDeleteHow can I submit the typed text to another URL for processing? In my case, I want user to type ahead and select the text they want to submit to do a search.It is currently going to the same URL used for getting the typeahead data.
ReplyDeleteWhat is bootstrap and why should you use bootstrap?
ReplyDeleteWhen i have multiple columns then what should be valueKey=?? i want search from multiple columns and also gives suggestion as per columns.
ReplyDeletewhere can i get the files boostrap.js and typeahead.js, i have looked all over the internet and can not for the life of me find the ones that work.
ReplyDeleteThanks for the Post Really needed it
ReplyDeleteHow can i add Html Tag in search box result
ReplyDeletehow can i integrate typehead in laravel 5.1
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletehow can i integrate typehead in codeignitor
ReplyDeleteAny body can share code for download? i ll' use for http://howcanisolve.com Thanks in advance
ReplyDeleteThese ways are very simple and very much useful,Thank you.
ReplyDeleteHire PHP developer in India | Professional website design company
This “php ustad” tutorial blog is designed for beginners – to learn php at home step by step using simple programming examples
ReplyDeletethank you , i want to know how i can use the json data used from search add to var
ReplyDeleteThank you for the information you wrote.
ReplyDeleteAndroid Mobile App Development in Ahmedabad
ReplyDeleteWeb 3.0 India is the most trusted Web3 Blockchain Development and NFT Development Company in USA. We also provides ICO, STO development for cryptocurrency.
https://web30india.com/
We appreciate you sharing this informative post with us. Continue providing us with helpful content.
ReplyDeletehttps://www.ownuxglobal.com/