php - Bootstrap typeahead not working -


i pulling stock symbols yahoo finance in json object , trying show them drop-down menu while user starts typing name of company or symbol in search box . typeahead not working drop down menu search box. think doing right.this code have far. appreciated.

quote.js

$(document).ready(function() {    // create autocomplete   $('#form-quote input[name=symbol]').typeahead({        // load autocomplete data suggest.php       source: function(query, callback) {           $.ajax({               url: '../suggest.php',               type: 'post',               datatype: 'json',               data: {                   symbol: query               },               success: function(response) {                   callback(response.symbols);                }           });       }   });    // load data via ajax when form submitted   $('#form-quote').on('click', function() {        // determine symbol       var symbol = $('#form-quote input[name=symbol]').val();        // send request quote.php       $.ajax({           url: 'quote.php',           type: 'post',           data: {               symbol: symbol           },           success: function(response) {               $('#price').text(response);           }       });         return false;   });  }); 

quote.php

<?php  //configuration require("../includes/config.php");  //if form submitted   if($_server["request_method"] == "post"){      $stock = lookup(strtoupper($_post["symbol"]));      if(empty($_post["symbol"])){          //echo "you must enter stock symbol";      }else if($_post["symbol"]){      $price = number_format($stock['price'], 2);      echo "a share of {$stock['name']} costs $$price";     } }  else{      // render portfolio render("stock_search.php", ["title" => "get quote"]); }    ?> 

quote_search.php

<form id = "form-quote" action="quote.php" method="post"> <fieldset>          <div class="control-group">         <input name="symbol" autofocus autocomplete="off"  placeholder="symbol"  type="text"/>     </div>      <div class="control-group">         <button type="submit" class="btn">get quote</button>     </div>  </fieldset> <div id="price"></div> <div id="suggestions"></div>  </form> <script type="text/javascript" src="js/quote.js" ></script> 

suggest.php

 <?php  // configuration require("../includes/functions.php");  // if form submitted if ($_server["request_method"] == "post") {     // load suggestion data $data = @file_get_contents("http://d.yimg.com/aq/autoc?query=  {$_post['symbol']}&region=us&lang=en-us&callback=yahoo.util.scriptnodedatasource.callbacks");      // parse yahoo data list of symbols $result = [];     $json = json_decode(substr($data, strlen('yahoo.util.scriptnodedatasource.callbacks('), -1));     foreach ($json->resultset->result $stock)         $result[] = $stock;      echo json_encode(['symbols' => $result]); }  ?> 

typeahead takes array of strings source

  // i.e.   ["intc", "goog", "fb", /* etc */] 

what script create array of whole objects yahoo returns

  // i.e.   [     {"symbol":"intc","name": "intel corporation","exch": "nms","type": "s","exchdisp":"nasdaq","typedisp":"equity"},     {"symbol":"intc.mx","name": "intel corporation","exch": "mex","type": "s","exchdisp":"mexico","typedisp":"equity"},     /* etc */   ] 

what need change suggest.php line:

foreach ($json->resultset->result $stock)         $result[] = $stock; 

becomes exmaple:

foreach ($json->resultset->result $stock)         $result[] = '('.$stock->symbol.') '.$stock->name; 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -