Typeahead Twitter

I am trying to use the Twitter type, but I have a problem. I do not know how typeahead passes the string to the server. Is this through the GET parameter? If so, what is the name of the parameter?

+6
source share
2 answers

The easiest with the GET parameter, you can select any parameter you want.

In JS:

$('#search').typeahead({ name: 'Search', remote: '/search.php?query=%QUERY' // you can change anything but %QUERY, it Typeahead default for the string to pass to backend }); 

In PHP (or any other server):

 $query = $_GET['query']; 

Hope you get the main idea.

+14
source

You might want to consider something like this, this is a very simple example of a remote data source. The get parameter in this example is 'q'

 // Get your data source var dataSource = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { url: 'path/to/your/url/json/datasource/?q=%QUERYSTRING', wildcard: '%QUERYSTRING' } }); // initialize your element var $typehead = $('#form input').typeahead(null, { source: dataSource }); // fire a select event, what you want once a user has selected an item $typehead.on('typeahead:select', function(obj, datum, name) { //your code here }); //////////////////////////////////// # in python (django) we get a query string using the request object passed through a view like this query = request.GET.get('q') or "" //the caveat [or ""] is just to prevent null exceptions /////////////////////////////////// # using php $query = ($_GET['q']) ? $_GET['q'] : ""; 
+1
source

Source: https://habr.com/ru/post/950769/


All Articles