2013-04-02 64 views
0

我拉從json對象的雅虎財務股票的符號,我試圖顯示他們作爲一個下拉菜單,而用戶開始在搜索框中鍵入公司名稱或符號。阿賈克斯typehead不能正常工作

Typeahead不作爲搜索框中的下拉菜單工作。這是迄今爲止的代碼。

如果我用$ go替換{$ _POST ['symbol']}來轉儲$ data或$ result在suggest.php中,例如我得到一個數組。所以,我在想,要麼建議。PHP回聲沒有返回任何東西或建議。PHP沒有被調用從quote.js文件。

如果我刪除了suggest.php中的if語句,並用goog替換{$ _POST ['symbol']},然後轉至http://localhost/suggest.php不應回顯打印內容?

任何幫助表示讚賞。我是網絡開發新手,並試圖學習。

quote.js

$(document).ready(function() { 

    // create autocomplete 
    $('#form-quote input[name=symbol]').typeahead({ 

     // load autocomplete data from 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 is submitted 
    $('#form-quote').on('click', function() { 

     // determine symbol 
     var symbol = $('#form-quote input[name=symbol]').val(); 

     // send request to 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 was submitted 

if($_SERVER["REQUEST_METHOD"] == "POST"){ 

    $stock = lookup(strtoupper($_POST["symbol"])); 

    if(empty($_POST["symbol"])){ 

     //echo "You must enter a 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 was 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 into a list of symbols 
$result = array(); 
    $json = json_decode(substr($data, strlen('YAHOO.util.ScriptNodeDataSource.callbacks('), -1)); 
    foreach ($json->ResultSet->Result as $stock) 
     $result[] = $stock; 

    echo json_encode(array('symbols' => $result])); 
} 

?> 

回答

4

我看到一些拼寫錯誤。如果這是一個測試服務器,你可能想在php中啓用錯誤。

無論如何,從這個代碼:

// load suggestion data 
$data = @file_get_contents("http://d.yimg.com/aq/autoc?query=GOOG&region=US&lang=en-US&callback=YAHOO.util.ScriptNodeDataSource.callbacks"); 

// parse yahoo data into a list of symbols 
$patt = array("/^YAHOO\.util\.ScriptNodeDataSource\.callbacks\(/","/\)$/"); 
$repl = array("",""); 
$json = json_decode(preg_replace($patt,$repl,$data)); 

foreach ($json->ResultSet->Result as $stock) 
    $result[] = $stock; 

echo json_encode(array('symbols' => $result)); 

我得到以下輸出:

{"symbols":[{"symbol":"GOOG","name":"Google Inc.","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"^UVSPY","name":"NASDAQ OMX Alpha GOOG vs. SPY","exch":"NAS","type":"I","exchDisp":"NASDAQ","typeDisp":"Index"},{"symbol":"GOOG-U.TI","name":"GOOGLE-A","exch":"TLO","type":"S","exchDisp":"TLX Exchange ","typeDisp":"Equity"},{"symbol":"GOOG11BF.SA","name":"GOOGLE -DRN MB","exch":"SAO","type":"S","exchDisp":"Sao Paolo","typeDisp":"Equity"},{"symbol":"GOOG.MX","name":"Google Inc.","exch":"MEX","type":"S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"GOOF.EX","name":"GOOGLE-A","exch":"EUX","type":"S","exchDisp":"EUREX Futures and Options Exchange ","typeDisp":"Equity"},{"symbol":"GGQ1.F","name":"GOOGLE-A","exch":"FRA","type":"S","exchDisp":"Frankfurt","typeDisp":"Equity"},{"symbol":"GGQ1.MU","name":"GOOGLE-A","exch":"MUN","type":"S","exchDisp":"Munich","typeDisp":"Equity"},{"symbol":"GGQ1.HA","name":"GOOGLE-A","exch":"HAN","type":"S","exchDisp":"Hanover","typeDisp":"Equity"},{"symbol":"GGQ1.DE","name":"GOOGLE-A","exch":"GER","type":"S","exchDisp":"XETRA","typeDisp":"Equity"}]} 

與您的數據獲取和分析糾正,如果你使用的引導2.1+,你應該能夠按this answer中所述實現對您的PHP代理的ajax調用。 (搜索「Bootstrap 2.1」的頁面,如果鏈接沒有跳到它。)

+0

感謝您的回覆。我的確得到了和你一樣的結果。有沒有辦法檢查ajax函數是否正常工作?我仍然無法獲得下拉菜單。 – tironci

+0

更新?你說我修正了PHP輸出,但你沒有接受我的答案。您是否按照我鏈接的答案更新了您的ajax電話? –

+0

我得到了您的更改結果,但是當我嘗試從ajax獲得結果時,我仍然無法獲得下拉菜單。 – tironci